[英]Get document references from fetched documents (Firestore)
I'm trying to get document references from the fetched documents of the collection "products" with 2 sets of code below but both return "undefined" .我正在尝试使用下面的 2 组代码从集合“产品”的提取文档中获取文档引用,但都返回“未定义” 。
Get single document:获取单个文档:
const docsSnap = await getDoc(
doc(db, "products/WuAC97j0avTAFNs1kvdf"),
);
console.log(docsSnap.data().ref); // undefined
Get multiple documents:获取多个文档:
const docsSnap = await getDocs(
collection(db, "products"),
);
console.log(docsSnap.docs[0].data().ref); // undefined
Are there any ways to get document references from the fetched documents?有没有办法从获取的文档中获取文档引用?
Remove ".data()" before ".ref" from both of the code to get document references .从两个代码中删除“.ref”之前的“.data()”以获取文档引用。
Get single document:获取单个文档:
const docsSnap = await getDoc(
doc(db, "products/WuAC97j0avTAFNs1kvdf"),
);
// Here
console.log(docsSnap.ref); // Tc
Get multiple documents:获取多个文档:
const docsSnap = await getDocs(
collection(db, "products"),
);
// Here
console.log(docsSnap.docs[0].ref); // Tc
In addition, you can also get document IDs by replacing ".ref" with ".id" :此外,您还可以通过将".ref"替换为".id"来获取文档 ID :
Get single document:获取单个文档:
const docsSnap = await getDoc(
doc(db, "products/WuAC97j0avTAFNs1kvdf"),
);
// Here
console.log(docsSnap.id); // WuAC97j0avTAFNs1kvdf
Get multiple documents:获取多个文档:
const docsSnap = await getDocs(
collection(db, "products"),
);
// Here
console.log(docsSnap.docs[0].id); // WuAC97j0avTAFNs1kvdf
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.