【发布时间】:2020-08-17 19:31:10
【问题描述】:
我有一个从 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()
);
我试图将此数据作为一个可观察的数据返回,但是虽然我可以看到填充正确数据库引用的路径,但什么时候说完我的配置数组只包含几千个“未定义”条目我在做什么错误的?我尝试了各种方法,但结果总是一样,所以我将问题浓缩到下面,希望得到一些帮助
//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();
【问题讨论】:
-
configs变量在switchMap主体中声明。这就是为什么return configs中的configs可能未定义 -
我应该在哪里申报?如果我删除
let configs =语句并用 return 替换它,结果是一样的。 -
@Dazza move
return configs;insideswitchMap -
@Kamran Khatti 我认为它已经在 switchmap 中了? (请原谅糟糕的代码格式,我已经更新了它)或者,你的评论中是否有明显的我遗漏的东西?
-
@Dazza 你在
return之前尝试过console.log(configs)以确保它具有这些值吗?
标签: angular rxjs angularfire mergemap