【发布时间】:2017-08-17 13:39:21
【问题描述】:
我正在使用 Ionic 3、Firebase 和 FirebaseAuth。我正在尝试使用当前经过身份验证的用户来检索该用户的对象列表。以下是我的尝试,但出现错误
getContacts 错误:TypeError:您在预期流的位置提供了“未定义”。您可以提供 Observable、Promise、Array 或 Iterable。
我在这里遗漏了一些东西,因为这似乎应该用每条新数据更新onNext。
我的尝试:
getContacts() {
this.contactList.length = 0;
this.data.getAuthenticatedUserContactList().subscribe(
(contact => {
this.contactList.push(contact);
}),
(error => console.error(`getContacts Error: ${error}`))
);
}
getAuthenticatedUserContactList() {
return this.afAuth.getAuthUser()
.flatMap(user => this.database.list(`contact-lists/${user.uid}`).take(1))
.flatMap((contactList) => {
if (contactList && contactList.length > 0) {
contactList.forEach(element => {
console.log(`ForEach: ${element.userId}`); // THIS LOGS THE IDS AS EXPECTED
return this.database.object(`/profiles/${uid}`, { preserveSnapshot: true }).take(1);
});
} else {
return Observable.throw(new Error("No bros here"));
}
});
}
任何帮助将不胜感激。
根据 forkJoin 建议的解决方案:
getContacts() {
this.contactList.length = 0;
this.data.getAuthenticatedUserContactList().subscribe(
(contact => {
contact.map(userContact => {
this.contactList.push(<UserProfile>(userContact));
});
}),
(error => console.error(`getContacts Error: ${error}`))
);
}
getAuthenticatedUserContactList() {
return this.afAuth.getAuthUser()
.mergeMap(user => this.database.list(`contact-lists/${user.uid}`).take(1))
.mergeMap((contactList) => {
return Observable.forkJoin(contactList.map(element => this.getProfileWithUid(element.userId).map(userProfile => userProfile.val())))
});
}
getProfileWithUid(uid: string) {
this.profileObject = this.database.object(`/profiles/${uid}`, { preserveSnapshot: true });
return this.profileObject.take(1);
}
【问题讨论】:
标签: javascript firebase firebase-authentication rxjs ionic3