【问题标题】:Error Database Triggered Cloud function with Promise带有 Promise 的错误数据库触发云功能
【发布时间】:2020-07-08 03:14:06
【问题描述】:

我正在尝试编写一个 Firebase 云函数,它现在主要做两件事:

1) 每当用户在线/离线时触发

2) 获取该用户的所有对话。

以下代码能够执行步骤 1,但在控制台中返回 Function 返回 undefined、expected Promise 或 value Error。

我不确定如何编写 Promises,这是我的第一个 Firebase 函数。请帮忙。

    exports.offlineHandler = functions.database.ref('/users/{userid}/status')
      .onUpdate((change, context) => {
        const status = change.after.val();
        const userId = context.params.userid;
        console.log('the user is now ', status, "with mobile no ", userId);
        // fetch a users conversations list
        if (status === "offline") {
          console.log("offline exec start");
          return fetchUserConversations(userId)
            .then(results => {
              console.log("offline exec end");
              for (result in results) {
                console.log("id is", result);
              }
              return results;
            }).catch(err => {
              console.error("An error has occurred.", err);
              return err;
            });
        } else {
          console.log("user came online");
        }
        return null;
      });

function fetchUserConversations(userId) {
  return admin.firestore().collection('users/${userId}/conversations').get()
    .then(snapshot => {
      var conversations = [];
      snapshot.forEach(doc => {
        console.log("This is conversation id => ", doc.id);
        conversations.concat(doc.id);
      });
      return conversations;
      //return conversations;
    }).catch(err => {
      console.error(err);
      return err;
    });
}

【问题讨论】:

    标签: node.js firebase google-cloud-firestore google-cloud-functions


    【解决方案1】:

    从语法上讲,您似乎只是在 fetchUserConversations(userId)... 之前缺少了一个 return。所以:

    return fetchUserConversations(userId).then(result=>{
    

    要了解有关 Cloud Functions 中的 Promise 的更多信息,您最好学习 sync, async, and promises 上的 Firebase 文档,以及 using promises in Cloud Functions 上 Doug Stevenson 的精彩视频系列。


    您的代码的一个更根本的问题是它不会对它为用户获取的对话做任何事情。您似乎正在尝试从 Cloud Function 中返回它们,但由数据库写入触发的 Cloud Functions 无法返回任何内容。此类函数由数据库写入触发,不是由用户操作触发。

    虽然在您的情况下是用户操作(重新上线)触发了 Cloud Function,但这是一个间接触发器,您无法从 Cloud Function 向用户返回任何内容。

    这里有两个主要选择:

    1. 将对话写入数据库中的某处,然后用户查找它们。

    2. 将 Cloud Functions 触发器类型更改为 HTTPS 或 Callable,然后在用户重新上线时从您的应用程序代码中调用此 Cloud Functions。

    【讨论】:

    • 是的,这是下一步。我需要将所有离线/删除所有在线用户写入/删除这些个人对话的firestore内的离线寄存器。但无法返回对话[] 本身。
    • 在 fetchUserConversations() 添加返回后得到以下错误 TypeError: Cannot read property 'then' of undefined at exports.offlineHandler.functions.database.ref.onUpdate (/srv/index.js :15:9) 在 cloudFunction (/srv/node_modules/firebase-functions/lib/cloud-functions.js:131:23) 在 /worker/worker.js:825:24 在 在 process._tickDomainCallback (internal /process/next_tick.js:229:7)
    • 按照建议更新到最新更改。请协助@Frank
    • 您还缺少returnreturn admin.firestore().collection('users/${userId}/conversations').get()。我真的建议花更多时间根据我在回答中提供的链接来掌握 Cloud Functions 中的 Promise,因为一旦您了解 Promise,就需要遵循错误消息留下的线索。
    • 我希望一旦您做出我建议的更改,错误消息就会更改。
    猜你喜欢
    • 2018-04-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-01
    • 2021-11-27
    相关资源
    最近更新 更多