【问题标题】:"TypeError: Cannot read property 'child' of undefined" when triggering cloud function触发云功能时“TypeError:无法读取未定义的属性'孩子'”
【发布时间】:2020-09-06 18:20:25
【问题描述】:

我有一个云功能,只要有新消息写入与该用户关联的数据库,就会向接收者发送通知。我似乎不断收到错误。

我是云功能的新手,似乎无法解决这个问题。

见下方的云功能

exports.sendNotification = functions.database.ref('/messages/{userId}/{messageId}')
.onWrite((snapshot,context) => {
    
    //get the userId of the person receiving the notification because we need to get their token
    const receiverId = context.params.userId;
    console.log("receiverId: ", receiverId);
    
    //get the user id of the person who sent the message
    //const senderId = context.data.child('senderUid').val();
    const senderId = snapshot.data.child('senderUid').val();
    console.log("senderId: ", senderId);
    
    //get the message
    const message = snapshot.data.child('messageBody').val();
    console.log("message: ", message);
    
    //get the message id. We'll be sending this in the payload
    const messageId = context.params.messageId;
    //const messageId = event.params.messageId;
    console.log("messageId: ", messageId);
    
    //query the users node and get the name of the user who sent the message
    return admin.database().ref("/Renters/" + senderId).once('value').then(snap => {
        const senderName = snap.child("houseName").val();
        console.log("senderName: ", senderName);
        
        //get the token of the user receiving the message
        return admin.database().ref("/users/" + receiverId).once('value').then(snap => {
      //const token = snap.child("messaging_token").val();
      const token = event.data.child('receiverToken').val();
            console.log("token: ", token);
            
            //we have everything we need
            //Build the message payload and send the message
            console.log("Construction the notification message.");
            const payload = {
                data: {
                    data_type: "direct_message",
                    title: "New Message from " + senderName,
                    message: message,
                    message_id: messageId,
                }
            };
            
            return admin.messaging().sendToDevice(token, payload)
                        .then(function(response) {
              console.log("Successfully sent message:", response);
              //using this return statement to avoid error
              return response.successCount;
                          })
                          .catch(function(error) {
                            console.log("Error sending message:", error);
                          });
        });
    });
});

【问题讨论】:

  • 对此的任何帮助将不胜感激。我有不同的方法从 DatabaseSnapshot 中检索数据库值,但没有一种方法对我有用。

标签: javascript firebase firebase-realtime-database google-cloud-functions


【解决方案1】:

我做了一些测试,documentationDataSnapshot 对象中没有属性data。由于这个调用,它会给undefined 值。因此,如果您对此调用任何内容,则会导致此类错误。

如果您将...snapshot.data.child('senderUid').val()... 更改为:

...snapshot.child('senderUid').val()...

不确定您为什么在此处添加此 data,但即使您在引用的路径中有名为“data”的子项,它也无法正常工作。

【讨论】:

    猜你喜欢
    • 2019-02-19
    • 2019-01-29
    • 2018-12-05
    • 2020-05-18
    • 1970-01-01
    • 2017-05-08
    • 1970-01-01
    • 2019-03-06
    • 2021-07-15
    相关资源
    最近更新 更多