我想组织发送有关将文档添加到 Firestore 的推送通知。我正在使用 node.js 的 firebase 站点示例中的代码。
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();
var message = {
notification: {
title: 'title!',
body: 'body'
},
topic: "all"
};
exports.createRequest = functions.firestore
.document('Requests/{RequestsId}')
.onCreate((snap, context) => {
console.log('We have a new request');
// Send a message to devices subscribed to the provided topic.
admin.messaging().send(message)
.then((response) => {
console.log('Successfully sent message:', response);
}).catch((error) => {
console.log('Error sending message:', error);
});
return 0;
});
当我尝试部署时出现错误:
Each then() should return a value or throw promise/always-return" for string
.then((response) => {
最佳答案
改变这个:
admin.messaging().send(message)
.then((response) => {
console.log('Successfully sent message:', response);
}).catch((error) => {
console.log('Error sending message:', error);
});
return 0;
});
进入这个:
return admin.messaging().send(message)
.then((response) => {
console.log('Successfully sent message:', response);
return null;
}).catch((error) => {
console.log('Error sending message:', error);
});
});
您需要正确地终止函数,这样您就可以避免因运行时间过长或无限循环的函数而产生过多的费用。
您可以使用以下方式终止您的函数:
Resolve functions that perform asynchronous processing (also known as "background functions") by returning a JavaScript promise.
Terminate HTTP functions with
res.redirect()
,res.send()
, orres.end()
.Terminate a synchronous function with a
return;
statement.
关于javascript - 用于 firebase 的 node.js 中的 messaging() 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56205173/