Firebase Cloud函数subscribeToTopic超时



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

你可以在文档的终止HTTP函数一节中了解更多。

最新更新