【问题标题】:Get "Undefined" for snapshot values获取快照值的“未定义”
【发布时间】:2017-10-05 03:43:59
【问题描述】:

log result

inside of the log error 我目前正在设置我的 Firebase Cloud 功能,以便在每次有新的孩子添加到“消息”时都有一个用户到用户的推送通知。对于每个用户,在这个结构“/User/UID/Token”中的一个节点中存储了一个通知。但是,在我在 Firebase 控制台的日志中,结果显示返回的值是“未定义”。这是我第一次使用 Node.js,所以一切都很新。任何帮助,将不胜感激。这是函数内部的内容

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

const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);

// Listens for new messages added to messages/:pushId
exports.messageWritten =     functions.database.ref('/messages/{pushId}').onWrite( event => {

console.log('Push notification event triggered');

//  Grab the current value of what was written to the Realtime   Database.
var valueObject = event.data.val();
console.log(valueObject.text,valueObject.toId);

return admin.database().ref(`/Users/${valueObject.toId}`).once('value').then(snapshot =>{
    console.log('the users name',snapshot.name)
    console.log('the users token',snapshot.token)
})


// Create a notification
const payload = {
    notification: {
        title:snapshot.name +' sent you a message', 
        body: valueObject.text,
        sound: "default"
    },
};

 //Create an options object that contains the time to live for the  notification and the priority
const options = {
    priority: "high",
    timeToLive: 60 * 60 * 24
};


  return admin.messaging().sendToDevice(snapshot.token, payload, options);
});

【问题讨论】:

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


    【解决方案1】:

    您的函数中有两个return 语句。第一个返回语句(发送消息)之后的代码将不会运行。

    const functions = require('firebase-functions');
    
    const admin = require('firebase-admin');
    admin.initializeApp(functions.config().firebase);
    
    // Listens for new messages added to messages/:pushId
    exports.messageWritten = functions.database.ref('/messages/{pushId}').onWrite(event => {
    
        console.log('Push notification event triggered');
    
        //  Grab the current value of what was written to the Realtime   Database.
        var valueObject = event.data.val();
        console.log(valueObject.text, valueObject.toId);
    
        return admin.database().ref(`/Users/${valueObject.toId}`).once('value').then(snapshot => {
            console.log('the users name', snapshot.val().name)
            console.log('the users token', snapshot.val().token)
    
            // Create a notification
            const payload = {
                notification: {
                    title: snapshot.val().name + ' sent you a message',
                    body: valueObject.text,
                    sound: "default"
                },
            };
    
            //Create an options object that contains the time to live for the  notification and the priority
            const options = {
                priority: "high",
                timeToLive: 60 * 60 * 24
            };
    
            return admin.messaging().sendToDevice(snapshot.val().token, payload, options);
        })
    });
    

    【讨论】:

    • 我刚刚尝试了您建议的代码,但它仍然未定义,我提供了问题中发生的屏幕截图
    • 是的,snapshot.token 不起作用。你可能想要snapshot.val().token。我强烈建议在继续之前阅读Firebase documentation。在那里度过几个小时,将为您节省大量时间和许多堆栈溢出问题。
    猜你喜欢
    • 1970-01-01
    • 2018-09-26
    • 1970-01-01
    • 2018-01-21
    • 1970-01-01
    • 1970-01-01
    • 2023-03-26
    • 1970-01-01
    • 2018-11-30
    相关资源
    最近更新 更多