为什么我的 Firebase 云函数进程从未完成?



我有这个函数来测试向所有用户发送通知。

export const sendTestingNotification = functions.https.onRequest((request, response) => {
const message = "Hello world";
const body = "This is body"
const getAllUsersPromise = admin.database().ref('/users').once('value')
const payload = {
notification: {
title: message,
body: body
}
}
return getAllUsersPromise.then(results => {
var tokens : string[] = [];
console.log("Child Snapshot count: ", results.numChildren())
results.forEach(childSnapshot => {
var childData = childSnapshot.val();
var instanceId = String(childData.instanceId)
if (childData.instanceId != null) { tokens.push(instanceId); }
})
console.log('final tokens = ',tokens," notification= ",payload);
return admin.messaging().sendToDevice(tokens, payload).then(response2 => {
console.log ("Done sending notification call. Entering callback.")
const tokensToRemove : string[] = [];
response2.results.forEach ((result, index) => {
const error = result.error;
if (error) {
console.error('Failure sending notification to instance id = ', tokens[index], error);
}
else {
console.log("Successfully send notification to ", tokens[index])
}
});
return Promise.all(tokensToRemove);
})
.catch(console.log.bind(console));
})
.catch(console.log.bind(console));
});

我在 firebase 数据库中只有一个用户,而该用户具有 instanceId。

以下是控制台日志:

Child Snapshot count: 1
final tokens = [ 'eMZHr5WgHmU:APA91bEKg8wAS5qYMxuSJqn...
Done sending notification call. Entering callback.
Successfully send notification to eMZHr5WgHmU:APA91bEKg8wAS5qYMxuSJqn...
Function execution took 60002 ms, finished with status: 'timeout'

我的函数执行还剩下什么才能正常完成?调用该函数的浏览器永远不会停止显示加载指示器。

如果我在返回 Promise 之前添加 response.send 浏览器加载完成。但是检查日志显示该过程仍在工作,并返回"函数执行耗时 60002 毫秒,完成状态:"超时"错误。我该如何解决这个问题?

对于 HTTP 类型函数,您需要向调用方发送响应以终止函数。 对于函数中的所有代码路径,您应该调用response.send()或将发送该响应的内容。 有关详细信息和示例,请参阅文档。 特别是,请阅读有关终止函数的部分。

最新更新