我正在成功地将数据保存到 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);
});
但是当我尝试检索它时,firestore 发送“没有这样的文件”。试图获取对象数组。
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);
});
这是数据库
编辑:我发现为了访问所有文档,您必须这样做。
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);
});
它进入 then 块,但随后 querySnapshot.forEach 不运行
好的,所以我找到了解决方案。
db.collection(`store/${userID}/products`)
.get()
.then((querySnapshot) => {
querySnapshot.forEach((doc) => {
console.log(doc.data());
});
});
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句