【问题标题】:Firebase Function writing undefined to FirestoreFirebase 函数将未定义写入 Firestore
【发布时间】:2021-06-24 18:19:39
【问题描述】:

我是新人,如果需要更多信息,请告诉我!

我正在尝试将 MessageBird 与 Firestore 集成。除了接受电话号码的“to”字段外,它可以将所有内容写入数据库。我正在记录该号码,并且它在控制台中正确输出,除了在我的 Firestore 中它被存储为undefined。如果有人能提供任何关于为什么会这样的见解,那将不胜感激。我被困了一段时间。 (附上出现问题的部分功能和控制台截图)

我最初尝试在doc.data()?.phoneNum 行上不使用await 并刚刚收到[object Promise]。现在它正在控制台中打印,我知道这与它无关。请帮忙!

admin.auth().listUsers().then((res) => {
    res.users.forEach((userRecord) => {
      try {
        const dbRef = firestore()
            .collection("collection").doc(userRecord.uid)
            .collection("collection").doc(document);
        dbRef.get().then((context) => {
          if (context.exists) {
            firestore()
              .collection("collection")
              .doc(userRecord.uid).get()
              .then(async (doc) => {
                number = await doc.data()?.field1;
                console.log("Sending message to: " + number);
              });
            firestore().collection("collection")
              .doc()
              .create({
                channelId: "id",
                type: "text",
                content: {
                  text: "message",
                },
                to: `${number}`,
              });
            functions.logger.log("SUCCESSFULLY SENT TO DB");
          } else {
            // console.log("**Unviable user**");
          }
        });
      } catch (error) {
        console.log(error);
      }
    });
  });

【问题讨论】:

    标签: typescript firebase google-cloud-firestore messagebird


    【解决方案1】:

    承诺似乎没有得到妥善处理。 .data() 不返回承诺,因此您不需要 await 那里。此外,我没有看到任何地方声明的 var 'number' ,所以它是未定义的。您可以尝试运行以下代码:

    admin.auth().listUsers().then(async (res) => {
        try {
            for (const userRecord in res) {
                const dbRef = firestore()
                    .collection("collection").doc(userRecord.uid)
                    .collection("collection").doc(document);
                const context = await dbRef.get()
                if (context.exists) {
                    const number = (await firestore().collection("collection").doc(userRecord.uid).get()).data().field1
                    console.log("Sending message to: " + number);
                    await firestore().collection("collection").add({
                        channelId: "id",
                        type: "text",
                        content: {
                            text: "message",
                        },
                        to: `${number}`,
                    });
                    functions.logger.log("SUCCESS");
                } else {
                    // console.log("**Unviable user**");
                }
            }
        } catch (error) {
    
        }
    });
    

    另外,forEach 不等待承诺解决,所以我使用了 for-in 循环。

    【讨论】:

    • for-in 循环似乎是我正在寻找的部分。作为一个新手程序员,Promise 把我搞糊涂了!我是stackoverflow的新手,如果问题/我的代码是个谜,对不起。
    猜你喜欢
    • 2018-05-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-04
    • 2018-12-20
    • 2021-09-12
    相关资源
    最近更新 更多