【问题标题】:Node.js TypeError: admin.firestore(...).collection(...).doc(...).get(...).data is not a functionNode.js 类型错误:admin.firestore(...).collection(...).doc(...).get(...).data 不是函数
【发布时间】:2023-02-01 01:40:40
【问题描述】:

我试图通过首先运行 if 函数来查看另一个文档是否具有特定属性来获取文档的 url 属性

const functions = require("firebase-functions");

// The Firebase Admin SDK to access Firestore.
const admin = require("firebase-admin");
admin.initializeApp();

exports.writeToClay = functions
    .region("europe-west1")
    .firestore
    .document("/reviews/{documentId}")
    .onWrite((change, context) => {
      // Grab the current value of what was written to Firestore.
      const websiteId = change.before.data().websiteId;
      console.log("websiteID:", websiteId);
      if (change.after.data().code == "1") {
        const url = admin.firestore().collection(
            "websites"
        ).doc(websiteId).get().data().url;
        console.log("url:", url);
      }
    });

【问题讨论】:

    标签: javascript node.js google-cloud-firestore


    【解决方案1】:

    get() 返回一个 Promise 并且没有 data() 方法。尝试重构代码,如下所示并处理承诺:

    exports.writeToClay = functions
      .region("europe-west1")
      .firestore
      .document("/reviews/{documentId}")
      .onWrite(async (change, context) => { // <-- async function
        const websiteId = change.before.data().websiteId;
        console.log("websiteID:", websiteId);
    
        if (change.after.data().code == "1") {
          // add await here
          const snap = await admin.firestore().collection("websites").doc(websiteId).get()
          const url = snap.data().url;
          console.log("url:", url);
        }
    
        return
      });
    

    【讨论】:

      猜你喜欢
      • 2018-09-17
      • 2019-05-28
      • 2018-09-22
      • 2023-03-23
      • 2015-11-05
      • 2019-02-05
      • 2018-09-29
      • 1970-01-01
      • 2018-12-16
      相关资源
      最近更新 更多