node.js - 函数返回未定义、预期的 Promise 或值 Firebase 日志错误

标签 node.js firebase google-cloud-functions

我正在尝试使用 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/

相关文章:

Firebase 函数解析请求正文,然后我才能在 Express 中处理它

mobile - 使用手机数据 (3g/4g) 时无法访问 Firebase 服务器

node.js - Firebase Firestore 事务处理极其缓慢(3-4 分钟)

google-cloud-platform - Google Cloud 函数访问 BigQuery

javascript - 回调是否保证 ES6 类构造函数中的 promise 得到解决或拒绝?

javascript - 如何显示书的内容

node.js - Node JS 的 cloud 9 ide 调试如何工作?

android - com.google.android.gms.internal.firebase-perf.zzq.zzb(boolean)' 在空对象引用上

node.js - Firestore 事务在单个事务中更新多个集合

node.js - WebFaction Node.Js 上的 BCrypt 未安装