如何了解Firebase云功能中的新数据?

时间:2017-05-18 12:11:50

标签: node.js firebase google-cloud-functions

您如何知道数据是否未发生变化以及Firebase Cloud功能的新变化?我已经尝试了几个不同的选项来标记如果数据刚刚更改,那么我不发送推送通知。我也尝试从快照_newData获取 和_data子节点比较数量,如果__newData更大,那么这是一个新记录,但它没有用。请告诉我怎么做。

示例代码的片段。

   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}'),则会报告错误,该字段不能为空。

我试过但是我没有得到好结果。

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')
    }

3 个答案:

答案 0 :(得分:3)

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

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');
        }
     });

答案 1 :(得分:1)

检查以下示例:

exports.Observedata = functions.database.ref("/location/{id}").onWrite((event) => {
  const id = event.params.id;
  const snap = event.data;
  if (!snap.exists() && snap.previous.exists()) {
    //deleted data
  } else if (snap.exists() && !snap.previous.exists()) {
    //newly addded data
  } else if (!snap.child("isactive").val() && snap.child("isactive").changed()) {
    //if the node isactive changed its value from false to true
  }
});

答案 2 :(得分:0)

除了此处已发布的内容之外,您拥有的另一个选项是使用changed()方法作为标志来指示给定引用是否已更改。例如:

exports sampleFunction = functions.database.ref('/userListEvents/${userId}')
  .onWrite(event => {
    var eventSnapshot = event.data;
    if (eventSnapshot.changed()) {
      // Here you know something has changed
    }
  });

虽然它没有告诉您更改,但这是一种了解给定节点下某些是否已更改的简单方法。有another example in the docs