我正在尝试使用 firebase-function 和 node.js 在我的应用程序上添加推送通知,并且一切正常,就像我收到发件人的通知一样。但我唯一担心的是日志给了我这个错误
Function returned undefined, expected Promise or value
这是我的代码:
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();
exports.sendNotification = functions.database.ref('/notifications/{user_id}/{notification_id}').onWrite((change, context) => {
const user_id = context.params.user_id;
const notification_id = context.params.notification_id;
console.log('We have a notification : ', user_id);
const afterData = change.after.val();
if (!afterData){
return console.log('A notification has been deleted from the database', notification_id);
}
const fromUser = admin.database().ref(`/notifications/${user_id}/${notification_id}`).once('value');
return fromUser.then(fromUserResult => {
const from_user_id = fromUserResult.val().from;
console.log('You have a new notification from: ', from_user_id);
const deviceToken = admin.database().ref(`/Users/${user_id}/device_token`).once('value');
return deviceToken.then(result => {
const token_id = result.val();
const payload = {
notification: {
title: "New Friend Request",
body: "You've received a new Friend Request",
icon: "default"
}
};
return admin.messaging().sendToDevice(token_id, payload).then(response => {
console.log('This was the notification feature');
});
});
});
});
我应该在这里返回什么以及它会放在哪里?我目前使用的是最新的 firebase CFM 1.0 版
最佳答案
此行可能导致错误消息:
return console.log('A notification has been deleted from the database', notification_id);
当命中此行时,您实际上是从函数返回 undefined
,因为那是 console.log()
返回的内容。相反,您应该在日志后返回 null
。
关于node.js - 函数返回未定义、预期的 Promise 或值 Firebase 日志错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49877553/