首页 > 解决方案 > 查找由外部程序引起的当前 firebase 修改的路径

问题描述

我将 Firebase(实时数据库)与应用程序一起使用,并且在某些时候数据库的值被应用程序修改。为了捕捉这个修改,我添加了一个监听器作为 Firebase 函数。代码如下所示:

exports.chooseWordsGeneration = 
  functions.database.ref("roomsTest").onWrite((change, context) => {
    // Get the path of the modification in the database
});

与数据库一起

当前数据库

因此,例如,如果应用程序修改了 test2 的值,我希望能够捕获整个路径,即“34/test2”。换句话说,我想知道哪个房间被修改了。我尝试使用change作为参数给出的变量,onWrite但我不是很成功。谢谢你的帮助。

标签: javascriptnode.jsfirebasefirebase-realtime-databasegoogle-cloud-functions

解决方案


如果您想轻松“捕获整个路径,因此'34 / test2'”您必须监听 的子子节点roomsTest,如下所示:

exports.chooseWordsGeneration = functions.database
  .ref('roomsTest/{roomId}/{testId}')
  .onWrite((change, context) => {
    console.log(context.params.roomId + '/' + context.params.testId);
    //....
    return false;  //to adapt
  });

请注意,这change.after.val()将为您提供testId节点的值。


推荐阅读