本文介绍了如何知道Firebase云功能中的新数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您如何知道数据是否未更改以及Firebase Cloud功能中的新增功能?我尝试了几种不同的选项来标记如果只是更改了数据,则不发送推送通知.我也尝试从快照_newData获取和_data子节点进行比较,并且如果__newData更大,则这是一条新记录,但是没有用.请告诉我该怎么做.

How do you know if the data is not changing and what's new in Firebase Cloud functions? I've tried several different options to flag that if the data is just changed, then I do not send push notification. Also I tried to get from snapshot _newData  And _data child nodes to compare the number, and if __newData is greater, then this is a new record, but it did not work. Please tell me how it can be done.

示例代码段.

   module.exports = functions.database.ref('/userListEvents/{userID}')
        .onWrite(event => {

            const snapshot = event.data;

            if (event.data.val() && !event.data.previous.val()) {
                console.log('send push notification');
            } else if (snapshot._data) {
                console.log('send push notification');
            } else {
                return console.log('data was removed');
            };
    }

参考

functions.database.ref('/cards/{cardID}/interestedUsers')
    .onWrite(event => {

已更新:此选项对我不起作用,因为如果我执行例如functions.database.ref ('/ userListEvents / {userID} / {eventID}'),则报告错误,该字段不能为空.

Updated: This option does not work for me, because if I do eg functions.database.ref ('/ userListEvents / {userID} / {eventID}'), then an error is reported that the field must not be empty.

我曾经尝试过,但是没有得到很好的结果.

I tried so but I didn't get a good result.

if (event.data.val() && !event.data.previous.val()) {
        console.log('send push notification');
    } else if (event.data.val() && event.data.previous.val()) {
        const newData = event.data.val();
        const previousData = event.data.previous.val();
        console.log('newData', newData, 'previousData', previousData);
        const newDataKeys = Object.keys(newData);
        const previousDataKeys = Object.keys(previousData);

        if (newDataKeys.length > previousDataKeys.length) {
            console.log('send push', newDataKeys.length, previousDataKeys.length);
        } else {
            return console.log('just update data', newDataKeys.length, previousDataKeys.length);
        }
    } else {
        return console.log('removed data')
    }

推荐答案

您需要使用event.data.current.val()而不是event.data.val()进行比较.示例:

You need to compare using event.data.current.val() not event.data.val(). Example:

exports.detectChange = functions.database.ref('/userListEvents/{userID}')
    .onWrite(event => {
        const crnt = event.data.current;
        const prev = event.data.previous;

        if (crnt.val() && !prev.val()) {
            // value created
            console.log('Created: send push notification');
        } else if (!crnt.val() && prev.val()) {
            // value removed
            console.log('Removed: send push notification');
        } else {
            // value updated
            console.log('Updated');
        }
     });

这篇关于如何知道Firebase云功能中的新数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-29 04:20
查看更多