Javascript Firebase云函数事件-有时无法获取更新事件的数据

Javascript Firebase云函数事件-有时无法获取更新事件的数据,javascript,firebase,firebase-realtime-database,google-cloud-functions,Javascript,Firebase,Firebase Realtime Database,Google Cloud Functions,我已经编写了firebase云函数来触发更新记录。有时我得到的记录与正在更新的记录不同。我正在下面添加我的代码。请检查附加的图像也 exports.onNotificationUpdate = functions.database.ref('/Notification/{userId}/{notificationId}/userResponse').onUpdate(event => { return admin.database().ref(`/Notification/${ev

我已经编写了firebase云函数来触发更新记录。有时我得到的记录与正在更新的记录不同。我正在下面添加我的代码。请检查附加的图像也

exports.onNotificationUpdate = functions.database.ref('/Notification/{userId}/{notificationId}/userResponse').onUpdate(event => {
    return admin.database().ref(`/Notification/${event.params.userId}/${event.params.notificationId}`).once('value').then(function (snapshot) {
        var notification = snapshot.val();

        if (!notification) {
            console.error("Notification not found on notification update");
            return;
        };
我还可以从父级获取通知对象,但我想知道问题的最佳方法以及此代码的问题

这是我在这里的第一个职位,请让我知道如果需要更多的信息。
谢谢

您不必在函数中调用
一次
,因为它已经在您正在侦听的位置返回了数据,只需侦听父节点即可

因此,您应该这样做:

exports.onNotificationUpdate = functions.database.ref('/Notification/{userId}/{notificationId}').onUpdate(event => {
        const notification = event.data.val(); 

        if (notification === null) {
            console.error("Notification not found on notification update");
            return null;
            //actually this would only be called in case of deletion of the Notification
        } else {
            //do something with the notification data: send Android notification, send mail, write in another node of the database, etc.
           //BUT return a Promise
           //notification const declared above is a JavaScript object containing what is under this node (i.e. a similar structure than your database structure as shown in the image within your post.)
        }
});
我建议您看看Firebase团队的以下三个视频:


另外,请注意,云函数已经更新,如果您使用的是1.0.0以上的CF版本,那么代码的第一行应该以不同的方式编写。请参见

非常感谢,请参见我需要通知变量not/Notification/{userId}/{notificationId}/object中的这个/Notification/{userId}/{notificationId}/userResponse。我在/Notification/{userId}/{notificationId}/userResponse上添加了更新事件。我得到了你的答案,我应该通过父节点调用获得通知吗?不,你应该在父节点级别侦听,即/
notification/{userId}/{notificationId}
。我已经更新了我的答案。此外,我允许我自己坚持:看一下这3个视频(至少是前两个),它们是任何开始使用云功能的人都必须看的。这不是浪费时间,而是真正的投资。你有时间考虑这个解决方案吗?你可以考虑接受它,谢谢。