【问题标题】:Not able to retrieve data from firebase firestore无法从 Firebase Firestore 中检索数据
【发布时间】:2020-08-01 22:21:57
【问题描述】:

我正在成功将数据保存到 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 没有运行

【问题讨论】:

    标签: javascript firebase google-cloud-firestore nosql


    【解决方案1】:

    好的,我找到了解决方案。

    db.collection(`store/${userID}/products`)
          .get()
          .then((querySnapshot) => {
            querySnapshot.forEach((doc) => {
              
              console.log(doc.data());
            });
          });
      
    
       
    

    【讨论】:

      猜你喜欢
      • 2019-09-24
      • 1970-01-01
      • 1970-01-01
      • 2022-01-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多