【发布时间】:2020-09-28 13:26:29
【问题描述】:
我是 Firebase 的新手。我现在正在尝试对我的集合执行查询,但我不知道如何。我有登录用户(登录后我有他的 ID)、类别和购买集合。
Category 集合有一个 userId 列,Purchase 有 CategoryId。
拥有 userId,我如何查询该用户的购买集合?我应该以某种方式在 Purchase 和 Category 之间进行一种内部连接,其中 Category.UserId 等于我拥有的 UserId。
我该怎么做?
提前致谢。
这是我当前的代码,但它不起作用:
fetchAllPurchaseHistory(userId: string) {
this.fetchCategories()
const userDocRef = firebase.firestore()
.collection('User')
.doc(userId);
const categoriesDocRef = firebase.firestore()
.collection('Category').where('UserId', '==', userDocRef);
this.fbSubs.push(this.db.collection('Purchase', ref => ref.where('CategoryId', '==', categoriesDocRef))
.snapshotChanges()
.pipe(map(docArray => {
return docArray.map(doc => {
return {
Id: doc.payload.doc.id,
UserId: doc.payload.doc.data()['UserId'],
Description: doc.payload.doc.data()['Description'],
Address: doc.payload.doc.data()['Address'],
Establishment: doc.payload.doc.data()['Establishment'],
Price: doc.payload.doc.data()['Price'],
CreatedDate: doc.payload.doc.data()['CreatedDate']
};
});
})).subscribe((ps: Purchase[]) => {
this.store.dispatch(new Finance.SetPurchaseHistory(ps));
}));
}
【问题讨论】:
标签: javascript angular firebase google-cloud-firestore angularfire2