我正在寻找关于这个我的问题的文件,但它看起来相当多
我有一个这样的集合集合(user1)=>uid=>document( collection(Product => uid ),nameUser .... bla bla)
我得到了所有产品的文档 user1 这很容易,但是我如何从用户 2、用户 3 中获取它并收集到所有用户的数组产品中?
我在下面试过这段代码,但它不明白
getData = async () => {
const a = [];
await firebase.firestore().collection('user').doc().collection('Product').onSnapshot(sanpham => {
sanpham.forEach((doc) => {
a.push({ id: doc.id, data: doc.data() })
})
})
this.setState({ data: a });
}
如果要从所有Product
集合中获取文档,可以使用集合组查询。
要使用它,你会这样做:
await firebase.firestore().collectionGroup('Product').onSnapshot(sanpham => {
sanpham.forEach((doc) => {
a.push({ id: doc.id, data: doc.data() })
})
})
如果您想知道user
产品属于哪个产品,您可以通过以下方式确定:
const productRef = doc.ref;
const productCollectionRef = productRef.parent;
const userRef = productCollectionRef.parent;
console.log(userRef.id);
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句