【问题标题】:Is there a way to store the last changes on real time database?有没有办法将最后的更改存储在实时数据库中?
【发布时间】:2020-09-11 22:54:37
【问题描述】:

我正在使用具有以下数据结构的实时数据库。我可以在 webapp 中添加更多机器,这些机器具有自动生成的 ID,并且所有机器都具有相同的数据结构。

machines
  -autogeneratedID1
    -id1 : 1
    -id2 : 2
  -autogeneratedID2
    -id1 : 4
    -Id2 : 3

我想跟踪 autogeneratedIDs 中 id 的更改,如果 autogeneratedID1 上的 id1 从 1 更改为 3,我想要返回带有时间戳的更改。

我正在尝试通过以下代码使用云功能:

exports.foo = functions.database.ref("machines/").onUpdate((change) => {
    const before = change.before.val();  // DataSnapshot before the change
    const after = change.after.val();  // DataSnapshot after the change


    console.log(before);
    console.log(after);
    return null;

但是之前和之后的对象返回给我数据库所有结构的 JSON。 我的第一个猜测是比较 2 个 JSON 对象并检测更改的位置并添加时间戳。然后我想将更改存储在数据库中。

有没有办法做到这一点?

最好的问候

【问题讨论】:

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


    【解决方案1】:

    终于找到了答案,这是我正在使用的代码:

    exports.foo = functions.database.ref("machines/{machineID}/{ValueID}").onUpdate((change,context) => {
        const before = change.before.val();  // DataSnapshot before the change
        const after = change.after.val();    // DataSnapshot after the change
    
        const machineID = context.params.machineID; //Machine ID
        const valueID = context.params.ValueID;     //Value ID
        
        console.log("MachineID: " + machineID + " " +"IDChanged: " + valueID + " " + "BeforeValue: " + before +" "+ "AfterValue: " + after);
    
    
        return null;
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多