如何从 cloud-firestore 中的集合接收所有文档对象?
How can I receive all document objects from a collection in cloud-firestore?
当我尝试从子集合中接收文档时,我得到了一个仅包含文档值的对象。
响应如下所示:
{
id: mealDocumentId,
name: 'Burger',
imageUrl: 'imgUrl1',
price: 3, quantity: 1
}
有没有办法从整个文档中接收对象?
我期待文档中的对象与此类似:
{
mealDocumentId1: {
name: 'Burger1',
imageUrl: 'imgUrl1',
price: 1,
quantity: 1,
},
mealDocumentId2: {
name: 'Burger2',
imageUrl: 'imgUrl2',
price: 2,
quantity: 2,
},
}
我的代码是 运行:
constructor(private db: AngularFirestore) { }
getAllMealDocuments() {
// this is the path to the subselection meals
const docRef = this.db.collection('tables').doc('documentId').collection('meals');
// try to get a document object and pass it.
return docRef.snapshotChanges().pipe(map(document => {
document.map(doc => {
return new Meal({
id: doc.payload.doc.id,
...(doc.payload.doc.data() as {})
});
});
}));
}
是否有其他方式接收所有文件?最好喜欢预期的甲酸盐?
谢谢!
听起来您正在寻找 documentChanges
stream,它(与 valueChanges
不同)在其输出中包含每个文档的 ID。
我建议查看 streaming collection data 上的 AngularFire 文档以了解流的类型以及它们之间的区别。
当我尝试从子集合中接收文档时,我得到了一个仅包含文档值的对象。
响应如下所示:
{
id: mealDocumentId,
name: 'Burger',
imageUrl: 'imgUrl1',
price: 3, quantity: 1
}
有没有办法从整个文档中接收对象?
我期待文档中的对象与此类似:
{
mealDocumentId1: {
name: 'Burger1',
imageUrl: 'imgUrl1',
price: 1,
quantity: 1,
},
mealDocumentId2: {
name: 'Burger2',
imageUrl: 'imgUrl2',
price: 2,
quantity: 2,
},
}
我的代码是 运行:
constructor(private db: AngularFirestore) { }
getAllMealDocuments() {
// this is the path to the subselection meals
const docRef = this.db.collection('tables').doc('documentId').collection('meals');
// try to get a document object and pass it.
return docRef.snapshotChanges().pipe(map(document => {
document.map(doc => {
return new Meal({
id: doc.payload.doc.id,
...(doc.payload.doc.data() as {})
});
});
}));
}
是否有其他方式接收所有文件?最好喜欢预期的甲酸盐? 谢谢!
听起来您正在寻找 documentChanges
stream,它(与 valueChanges
不同)在其输出中包含每个文档的 ID。
我建议查看 streaming collection data 上的 AngularFire 文档以了解流的类型以及它们之间的区别。