Firebase 云函数 subscribeToTopic 超时

Firebase Cloud Function subscribeToTopic timeout

我使用示例 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);
  })

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

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 部分阅读更多相关信息。