函数返回了未定义的,预期的Promise或值Firebase日志错误 [英] Function returned undefined, expected Promise or value Firebase log error
问题描述
我正在尝试使用firebase-function和node.js在我的应用程序上添加推送通知,并且一切正常,就像我从发件人处收到通知一样.但我唯一关心的是日志给了我这个错误
Im trying to add a push notification on my app using firebase-function and node.js and all its all working fine, like I got notification from the sender. but my only concern is that the log gave me this error
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
what should I return here and where it will be place? I am currently using the latest firebase CFM version 1.0
推荐答案
此行可能导致错误消息:
This line could be causing the error message:
return console.log('A notification has been deleted from the database', notification_id);
单击此行时,实际上是从函数中返回undefined
,因为这是console.log()
返回的结果.相反,您应该只在日志后return null
.
When this line is hit, you're effectively returning undefined
from the function, because that's what console.log()
returns. Instead, you should just return null
after the log.
这篇关于函数返回了未定义的,预期的Promise或值Firebase日志错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!