如何解决如何合并来自引用数组的角射
我有一个从angularfire检索的引用数组
this.userIds$ = this.users$.switchMap(email =>
db.list('/USERS/',ref => {
let temp = email ? ref.orderByChild('EMAIL').startAt(email).endAt(email + "\uf8ff") : ref
console.log('carrot');
return temp;
}
).snapshotChanges()
);
我试图以可观察的方式返回此数据,但是尽管我可以看到路径填充了正确的数据库引用,但说完了之后,我的configs数组仅包含数千个“未定义”条目,我在做什么错误?我尝试了各种方法,但结果始终是相同的,因此我将问题压缩到下面,并希望寻求帮助
//Subscribe to the output of user ids,when we get a hit (delivered as an array of Observable DataSnapshot) loop through and subscribe to all the children of this location
this.userConfigs$ = this.userIds$.pipe(
switchMap(itemIds => {
let configs = itemIds.map(itemId => {
let pathOrRef = '/AIS/USERSAVEDCONFIGS/' + itemId.key;
console.log('tomato');
db.list(pathOrRef).snapshotChanges(['child_added']);
});
return configs;
})
);
然后我将整个事情拉开序幕
this.userConfigs$.subscribe();
解决方法
您传递给switchMap
的函数应该返回一个Observable,而您正在返回一个数组。我认为TypeScript编译器应该对此发出警告。
如果我的理解正确,您想获取一个itemIds数组,对每个项目进行数据库调用,然后返回一个pathOrRef
数组。
如果是这样,应该可以执行以下操作:
- 使用
from()
一次发射一次userIds
- 使用
map()
将项目转换为pathOrRef
- 使用
switchMap()
进行数据库调用 - 使用
mapTo()
返回pathOrRef
- 使用
toArray()
将排放物排成一列
this.userConfigs$ = this.userIds$.pipe(
switchMap(itemIds => from(itemIds).pipe(
map(itemId => `/AIS/USERSAVEDCONFIGS/${itemId}`),switchMap(pathOrRef => fakeDatabaseCall(pathOrRef).pipe(mapTo(pathOrRef)))
)),toArray()
);
以下是StackBlitz
上的示例版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。