firebase - Firebase 云功能总是在日志中超时
问题描述
我正在使用此链接中的代码:https ://codelabs.developers.google.com/codelabs/firebase-cloud-functions/#9
并且总是在云功能的日志中超时,这是一个代码:
exports.sendPatrola = functions.database.ref('/test/tmp')
.onUpdate((change, context) => {
const original = change.after.val();
var payload = {
data: {
id: String(original.id),
x: String(original.x),
y: String(original.y),
dat: String(original.dat)
}
};
var options = {
priority: 'high',
contentAvailable: true,
timeToLive: 60 * 1
};
let tokens = []; // All Device tokens to send a notification to.
// Get the list of device tokens.
return admin.database().ref('keys').once('value').then((allTokens) => {
if (allTokens.val()) {
// Listing all tokens.
tokens = Object.keys(allTokens.val());
// Send notifications to all tokens.
return admin.messaging().sendToDevice(tokens, payload, options);
}
return {results: []};
}).then((response) => {
return cleanupTokens(response, tokens);
}).then(() => {
console.log('Notifications have been sent and tokens cleaned up.');
return null;
});
}
错误消息是:函数执行耗时 60002 毫秒,完成状态:“超时”
这里有什么问题?
解决方案
请尝试这种方式,该函数cleanupTokens
应该返回一个承诺:
exports.sendPatrola = functions.database.ref('/test/tmp').onUpdate((change, context) => {
const original = change.after.val();
var payload = {
data: {
id: String(original.id),
x: String(original.x),
y: String(original.y),
dat: String(original.dat)
}
};
var options = {
priority: 'high',
contentAvailable: true,
timeToLive: 60 * 1
};
let tokens = [];
return admin.database().ref('keys').once('value').then((allTokens) => {
if (allTokens.val()) {
tokens = Object.keys(allTokens.val());
return admin.messaging().sendToDevice(tokens, payload, options);
} else {
const result = [];
return result;
}
}).then((response) => {
return cleanupTokens(response, tokens);
}).then(() => {
console.log('Notifications have been sent and tokens cleaned up.');
return null;
});
});
推荐阅读
- python - Paraview VtkPolyData 为线段添加宽度
- java - JDBC连接尝试与资源使用
- python - 如何使用 Python 从 Excel 导出中的超链接下载文件
- javascript - 在 Nestjs 中构建存储库和服务的最佳方式
- regex - 正则表达式解析多行字符串
- google-cloud-platform - 在 BigQuery 中使用 BYTES?
- tailwind-css - Tailwind - 如果我使用图层,css 规则是不可见的
- bluej - bluej 语法高亮的颜色在哪里定义?
- javascript - Material UI:解析错误:标识符“Switch”已被声明
- zsh - iTerm2鼠标报告输出符号