【问题标题】:Firebase Cloud Function subscribeToTopic timeoutFirebase 云函数 subscribeToTopic 超时
【发布时间】:2021-11-01 20:16:24
【问题描述】:

我使用示例 Firebase 文档创建了以下云函数:

export const subscribeToTopic = functions.https.onRequest((request, response) => {
  const topic = "weeklySurvey";
  const registrationTokens = [
    "c3UXI...ktYZ" // token Truncated here for readability
  ];
  return admin.messaging().subscribeToTopic(registrationTokens, topic)
      .then((response) => {
        console.log("Successfully subscribed to topic:", response);

        //Update: Tried adding next line to avoid timeout error

        return response.status(200).send("Subscribed!");

        //But this returns a predeploy error:
        //Property 'status' does not exist on type 'MessagingTopicManagementResponse'.

      })
      .catch((error) => {
        console.log("Error subscribing to topic:", error);
        return response.status(500).send("Something went wrong");
      });
});

函数执行成功没有错误:

subscribeToTopic
Successfully subscribed to topic: { successCount: 1, failureCount: 0, errors: [] }

...然后在 60 秒时超时:

subscribeToTopic
Function execution took 60002 ms, finished with status: 'timeout'

我是否缺少在subscribeToTopic 完成后终止函数的代码? Firebase 文档代码参考MessagingTopicManagementResponse,或许暗示我这里需要更多?

admin.messaging().subscribeToTopic(registrationTokens, topic)
  .then((response) => {
    // See the MessagingTopicManagementResponse reference documentation
    // for the contents of response.
    console.log('Successfully subscribed to topic:', response);
  })

【问题讨论】:

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


    【解决方案1】:

    您不是通过发回响应来终止函数。尝试添加返回响应语句,如下所示:

    return admin.messaging().subscribeToTopic(registrationTokens, topic)
    // ^^ return statement here
      .then((res) => {
        console.log('Successfully subscribed to topic:', response);
    
        return response.send("Subscribed!")
      }).catch((e) => {
        console.log("Error", e)
        return response.send("Something went wrong")
      })
    

    您可以在文档的Terminate HTTP Functions 部分阅读更多相关信息。

    【讨论】:

    • 我最初也是这么想的……所以我在发帖之前就尝试了 res.status() res.send()、res.end() 的各种迭代。在 catch 块中返回 status() 似乎没问题。但是在 then() 块中放置任何类型的 response.status() 都会在部署时返回以下错误:Property 'status' does not exist on type 'MessagingTopicManagementResponse'.
    • @Swifter 抱歉,我没有看到你的变量名是什么。它应该是response.send() 而不是res.send()。我在我的答案中重命名了它。还将 FCM 响应更改为 res
    • 注明。也就是说,response.status()、response.send() 和 response.end() 都返回 Property ['status'/'send'/'end'] does not exist on type MessagingTopicManagementResponse 预部署错误。
    • @Swifter 你能用你拥有的最新功能代码更新你的问题吗?请注意,还有另一个名为 response 的变量(由 subscribeToTopic 返回)。也改变了
    • @Swifter 将 .subscribeToTopic(registrationTokens, topic).then((response) 更改为 .subscribeToTopic(registrationTokens, topic).then((fcmResponse) 否则 response 被声明两次......一次在函数顶部。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-11-30
    • 2023-04-02
    • 1970-01-01
    • 2022-06-29
    • 2018-05-17
    • 2020-06-09
    • 2018-04-14
    相关资源
    最近更新 更多