[英]Not able to retrieve data from firebase firestore
I am saving data to firebase successfully我正在成功将数据保存到 firebase
const productsRef = db.collection("store").doc(userID).collection("products");
productsRef
.doc(productID)
.set({
userID: userID,
productCatagory: productCatagory.value,
productCode: productID,
productName: productName.value,
price: price.value,
description: description.value,
time: time,
})
.then(function () {
console.log("Document successfully written!");
})
.catch(function (error) {
console.error("Error writing document: ", error);
});
But when i try to retrieve it firestore sends "No such document".但是当我尝试检索它时,firestore 发送“没有这样的文件”。 Trying to get the array of objects.试图获取对象数组。
db.collection("store")
.doc(userID)
.get()
.then((doc) => {
if (doc.exists) {
console.log(doc.data());
} else {
// doc.data() will be undefined in this case
console.log("No such document!");
}
})
.catch(function (error) {
console.log("Error getting document:", error);
});
Here is the db这是数据库
Edit: I found out that in order to access all documents you have to do it this way.编辑:我发现为了访问所有文件,你必须这样做。
db.collection("store")
.get()
.then((querySnapshot) => {
querySnapshot.forEach((doc) => {
// doc.data() is never undefined for query doc snapshots
console.log(doc.data());
});
})
.catch((e) => {
console.log(e);
});
It goes into the then block but then querySnapshot.forEach doesnt run它进入 then 块,但随后 querySnapshot.forEach 不运行
Okay so i found the solution.好的,所以我找到了解决方案。
db.collection(`store/${userID}/products`)
.get()
.then((querySnapshot) => {
querySnapshot.forEach((doc) => {
console.log(doc.data());
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.