【问题标题】:How to send firebase notifications using nodejs?如何使用nodejs发送firebase通知?
【发布时间】:2020-03-24 04:02:51
【问题描述】:

我想自动实现推送通知,我使用了 javascript (node.js) 但我收到了这个错误

Function returned undefined, expected Promise or value

我不是 node js 开发者,我是 Flutter 开发者,我不知道什么是 promise。

这是我的代码:

const functions = require('firebase-functions');

const admin = require('firebase-admin');


admin.initializeApp(functions.config().firebase);

var notificationMessageData;

exports.fcmTester = functions.firestore.document('posts/{postID}').onCreate((snapshot , context) => {
    notificationMessageData = snapshot.data();

    admin.firestore().collection('pushTokens').get().then(async (snapshot) => {
        var tokens = [];

        if (snapshot.empty) {
            console.log('No Devices');
        } else {
            for (var token of snapshot.docs) {
                tokens.push(token.data().tokenID);
            }

            var payload = {
                "notification": {
                    "title": "from" + notificationMessageData.writer,
                    "body": "from" + notificationMessageData.name,
                    "sound": "default"
                },
                "data": {
                    "sendername": notificationMessageData.writer,
                    "message": notificationMessageData.name
                }
            }

            return await admin.messaging().sendToDevice(tokens , payload).then((response) => {
                console.log('nice');
            }).catch((err) => {
                console.log(err);
            })
        }
    })
})




一切正常,我上传它没有任何问题,但是当将文档添加到帖子集合时,它会在日志中输出上述错误。

我创建了一个用户注册表单,我已经注册了用户并将他们的令牌 ID 放入一个名为 pushTokens 的集合中,然后为该集合中的每个用户发送通知,但这不起作用。

【问题讨论】:

    标签: node.js firebase firebase-cloud-messaging google-cloud-functions firebase-admin


    【解决方案1】:

    你的代码有两个问题:

    1. 您不会返回异步 Firebase 方法(get()sendToDevice())返回的承诺;
    2. 您将async/await 的使用与then() 方法混淆了。

    我建议你观看Firebase video series关于“JavaScript Promises”的3个官方视频,然后你首先尝试使用then()方法正确chain你的Promises并返回链。

    以下代码应该可以工作。

    exports.fcmTester = functions.firestore.document('posts/{postID}').onCreate((snapshot, context) => {
        const notificationMessageData = snapshot.data();
    
        return admin.firestore().collection('pushTokens').get()
            .then(snapshot => {
                var tokens = [];
    
                if (snapshot.empty) {
                    console.log('No Devices');
                    throw new Error('No Devices');
                } else {
                    for (var token of snapshot.docs) {
                        tokens.push(token.data().tokenID);
                    }
    
                    var payload = {
                        "notification": {
                            "title": "from" + notificationMessageData.writer,
                            "body": "from" + notificationMessageData.name,
                            "sound": "default"
                        },
                        "data": {
                            "sendername": notificationMessageData.writer,
                            "message": notificationMessageData.name
                        }
                    }
    
                    return admin.messaging().sendToDevice(tokens, payload)
                }
    
            })
            .catch((err) => {
                console.log(err);
                return null;
            })
    
    });
    

    那么,在体验了then()(和catch())对异步方法的“管理”之后,你可以尝试一下async/await:再次,Doug Stevenson 的这个official video 会很有帮助.

    【讨论】:

    • @thecoder 嗨,你有机会看看这个答案吗?谢谢。
    【解决方案2】:

    上面写着:

    Function returned undefined, expected Promise or value
    

    您是否尝试过从您的函数中返回nulltruefalse 等?

    promise 是您在程序中传递的东西,等待它变成真正的价值。

    似乎错误消息的重点是希望您从函数中返回某些内容。在不知不觉中,这是我能给出的最好建议。

    【讨论】:

    • 我已经按照你所说的做了,但仍然没有任何效果,它给了我同样的错误
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-03-23
    • 1970-01-01
    • 2018-03-09
    • 1970-01-01
    • 1970-01-01
    • 2018-02-28
    • 1970-01-01
    相关资源
    最近更新 更多