【发布时间】:2017-01-20 18:44:41
【问题描述】:
我有一个 Ionic 2 应用程序,它的功能是通过 observables 聚合一堆数据。数据回来了……除了第一次返回时它并不总是完整的。几秒钟后,我的数据再次返回,除了两个实例现在都填充了完整的数据。这真的很奇怪,我不确定是什么原因造成的。
这是我的功能:
getUserStories(data) {
return this._af.database
.object(`/social/users/${data.id}`)
// Switch to the joined observable
.switchMap((user) => {
let connections = [];
let connectionKeys = Object.keys(user.connections);
return Observable.combineLatest(
connectionKeys.map((connectionKey) => this._af.database
.object(`/social/users/${connectionKey}`)
),
(...connections) => {
connectionKeys.forEach((connectionKey, index) => {
this._af.database
.object(`/social/user_stories_seen/${connectionKey}/${data.id}`).subscribe(data => {
// Iterate over the connections and append the correct "last_seen" variable
connections.forEach((connection, index) => {
if(connection.$key === connectionKey) {
connections[index]['last_seen'] = data;
}
})
});
})
return connections;
});
})
}
这是调用此功能的视图:
ionViewDidLoad() {
// Get the user from storage and get all the connections
this.storage.get('user').then(data => {
//Get the user profile
this._users.getUserStories({id: data.id}).subscribe(stories => {
console.log('stories', stories);
});
})
}
还有其他人遇到过这个问题吗?
【问题讨论】:
标签: angular firebase firebase-realtime-database ionic2