gpt4 book ai didi

javascript - switchMap 返回一项

转载 作者:行者123 更新时间:2023-12-03 02:12:41 27 4
gpt4 key购买 nike

我正在尝试使用 switchMap 运算符切换可观察值:

return this.db.list(`UserPlaces/${this.authData.auth.auth.currentUser.uid}`, {
query: {
orderByChild: 'deleted',
equalTo: false
}
})
.map((locations:any)=>{
console.log(JSON.stringify(locations,null,2));
return locations.map(location=>{
return location.$key;
});
}).flatMap(locations=>{
return locations
}).switchMap(location=>{
console.log(location);
return this.db.object(`Devices/${location}`)
})
.do(console.log);

我得到一个可观察值,而不是一组可观察值。

I need to get array of "this.db.object(Devices/${location})"

如何解决这个问题?

谢谢。

最佳答案

您可能想要forkJoin可观察量以将它们放入一次提交中。你可以这样做:

return this.db.list(`UserPlaces/${this.authData.auth.auth.currentUser.uid}`, {
query: {
orderByChild: 'deleted',
equalTo: false
}
})
.map((locations:any)=>{
console.log(JSON.stringify(locations,null,2));
return locations.map(location=>{
return location.$key;
});
}).switchMap(locationKeys =>{
console.log(location);
const locationObs = locationKeys.map((key) => this.db.object(`Devices/${key}`);
return Observable.forkJoin(...locationObs);
})
.reduce((acc, location) => [...acc, location], []);
.do(console.log);

关于javascript - switchMap 返回一项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49490604/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com