Javascript 快照始终未定义

Javascript 快照始终未定义,javascript,firebase,firebase-cloud-messaging,google-cloud-functions,Javascript,Firebase,Firebase Cloud Messaging,Google Cloud Functions,因此,我尝试使用Firebase云消息向我的应用程序发送推送通知。然而,由于某种奇怪的原因,它似乎一直在失败。我正在使用onCreate方法来侦听特定路径下节点的创建。它是从onCreate方法中收集的信息中获取UID,但是当我尝试从其他节点获取用户信息以获取fcmToken时,我可以发送通知。它总是说未定义 我的index.js如下所示: //listen for reply notification and then trigger a push notification exports.o

因此,我尝试使用Firebase云消息向我的应用程序发送推送通知。然而,由于某种奇怪的原因,它似乎一直在失败。我正在使用onCreate方法来侦听特定路径下节点的创建。它是从
onCreate
方法中收集的信息中获取
UID
,但是当我尝试从其他节点获取用户信息以获取fcmToken时,我可以发送通知。它总是说未定义

我的index.js如下所示:

//listen for reply notification and then trigger a push notification
exports.observeNotification = functions.database.ref('/notifications/{uid}/{commentId}/')
    .onCreate(event => {
        // Grab the current value of what was written to the Realtime Database.
        //const notification = event.data.val();
        console.log('User ID is: ', event.params.uid);
        console.log('Comment ID Is: ', event.params.commentId);
        return admin.database().ref('/users/' + event.params.uid).once('value', snapshot => {
            var userWeAreSendingTo = snapshot
            console.log('User ID is: ', userWeAreSendingTo);

            console.log('FCM Token of that user is: ', userWeAreSendingTo.fcmToken);

            var payload = {
                notification: {
                    title: "",
                    body: snapshot.content
                }
            };
            admin.messaging().sendToDevice(userWeAreSendingTo.fcmToken, payload)
                .then((res) => {
                    // Response is a message ID string.
                    console.log('Successfully sent message:', res);
                    return true
                })
                .catch((error) => {
                    console.log('Error sending message:', error);
                    return true
                })


        })
    })
这是数据库中用户节点下的子节点的快照

   "9mzAHeX3lcdzriPdC4TfbRTkaUm2" : {
      "fcmToken" : "coC8uorosgc:APA91bGur2vvH4fwIProh87pUzVw0jYTOOFW3KfqWRVk4WdX0x8M1iBFwg28wM3tFyB5iRrowTWCZ_45oGwo0_7BFD6YvULE30NNZXxvE2O2XLjlLd_fqYwMfkndOqSUem2HqO-qvNcZ",
      "profilePic" : "https://firebasestorage.googleapis.com/v0/b/eventful-3d558.appspot.com/o/profile_images%2F832F8156-C14A-4EF3-86EB-D8F4CFC784E5.PNG?alt=media&token=b9d11ea1-8cb3-4d0f-b111-b0baf567ac7b",
      "username" : "lol123"
    }
这是控制台日志的屏幕截图


userWeAreSendingTo
没有您认为的值--您应该将
snapshot.val()
分配给该变量,而不仅仅是
snapshot
。我相信这会解决您的问题,因为“快照”是关于数据的信息,而不是数据本身
snapshot.val()
提供了快照位置的实际数据。天哪,我太笨了,谢谢你,伙计,我想什么都没做哦,等等,它成功了