如何根据 Google Cloud Functions 上初始路径的参考值将节点克隆到另一个路径?

发布时间:2023-03-02 / 作者:清心寡欲
本文介绍了如何根据 Google Cloud functions 上初始路径的参考值将节点克隆到另一个路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将原始"节点的数据(一旦创建数据)克隆到基于原始节点路径的路径.

这是我的数据结构:

根目录:{门: {111111111111:{MAC地址:111111111111",inRoom: "-LBMH_8KHf_N9CvLqhzU",//我需要这个值作为克隆的路径插入:{//我在这里创建了几个key: pair",比如:1525104151100:是的,1525104151183:真的,}}},房间:{-LBMH_8KHf_N9CvLqhzU:{插入:{//我希望函数在这里克隆相同的数据:1525104151100:是的,1525104151183:真的,}}}

我的云功能现在是这样的:

exports.updateRoom = functions.database.ref('/doors/{MACaddress}/ins').onWrite((change, context) => {const beforeData = change.before.val();//写入前的数据常量 afterData = change.after.val();//写入后的数据const roomPushKey = change.before.ref.parent.child('/inRoom');console.log(roomPushKey);//这是检索所有关于 ref "inRoom" 的信息,但不是它的值...

问题 1) 我怎样才能得到这个 ref/node 的值?

我的代码继续尝试获取这样的值.

 roomPushKey.once('child_added').then(function(dataSnapshot) {让 snapVal = dataSnapshot.val();控制台.log(snapVal);});

问题 2(我认为基本上是问题 1 的改写):如何使 snapVal 超出 then. 方法的范围?

 return change.after.ref.parent.parent.parent.child('/rooms').child(snapVal).child('/ins').set(afterData);//snapVal 应该高于});

错误信息:ReferenceError: snapVal is not defined

解决方案

以下应该可以.

const admin = require("firebase-admin");…………export.updateRoom = functions.database.ref('/doors/{MACaddress}').onWrite((change, context) => {常量 afterData = change.after.val();//写入后的数据常量 roomPushKey = afterData.inRoom;常量 ins = afterData.ins;常量更新 = {};更新['/rooms/' + roomPushKey] = ins;返回 admin.database().ref().update(updates);}).catch(错误 => {控制台日志(错误);//+ 必要时进行其他错误处理});

这里有一些解释:

您通过读取写入后的数据"作为对象来获取roomPushKey:roomPushKey = afterData.inRoom.你不需要做 roomPushKey.once('child_added').then()

一旦你有了 roomPushKey,你就可以在 rooms 节点中创建一个新的子节点,方法是使用 update() 并使用方括号表示法,它允许您分配节点的 id(即 roomPushKey).

请注意,您也可以这样做:

return admin.database().ref('/rooms/' + roomPushKey).set(ins);

还要注意,您必须导入 firebase-admin 才能返回 admin.database().ref()...

最后,我建议您观看 Firebase 团队的以下三个视频:youtube.com/watch?v=7IkUgCLr5oA&t=517s &youtube.com/watch?v=652XeeKNHSk&t=27s &youtube.com/watch?v=d9GrysWH1Lc.开始为 Cloud Functions 编码的任何人都必须这样做.

I am trying clone an "original" node's data (as soon as I create the data) to a path that is based on the original node's path.

This is my data structure:

root: { 
  doors: {
    111111111111: {
       MACaddress: "111111111111",
       inRoom: "-LBMH_8KHf_N9CvLqhzU", // I will need this value for the clone's path
       ins: {
          // I am creating several "key: pair"s here, something like:
          1525104151100: true,
          1525104151183: true,
       }
    }
  },
  rooms: {
    -LBMH_8KHf_N9CvLqhzU: {
      ins: {
        // I want the function to clone the same data here:
        1525104151100: true,
        1525104151183: true,
      }
    }
  }

My cloud function is now like this:

exports.updateRoom = functions.database.ref('/doors/{MACaddress}/ins').onWrite((change, context) => {
    const beforeData = change.before.val(); // data before the write
    const afterData = change.after.val(); // data after the write
    const roomPushKey = change.before.ref.parent.child('/inRoom');
    console.log(roomPushKey); // this is retrieving all the info about the ref "inRoom" but not its value... 

Question 1) How can I get to this ref/node's value?

My code goes on by trying to get the value like this.

    roomPushKey.once('child_added').then(function(dataSnapshot) {
        let snapVal = dataSnapshot.val();
        console.log(snapVal);
});

Question 2 (which I think is basically question 1 rephrased): How can I get the snapVal outside the then. method's scope?

    return change.after.ref.parent.parent.parent.child('/rooms')
.child(snapVal).child('/ins').set(afterData);
      // snapVal should go above
   });

Error message: ReferenceError: snapVal is not defined

解决方案

The following should work.

const admin = require("firebase-admin");
....
....

exports.updateRoom = functions.database.ref('/doors/{MACaddress}').onWrite((change, context) => {
    const afterData = change.after.val(); // data after the write

    const roomPushKey = afterData.inRoom;
    const ins = afterData.ins;

    const updates = {};
    updates['/rooms/' + roomPushKey] = ins;
    return admin.database().ref().update(updates);

}).catch(error => {
    console.log(error);
    //+ other rerror treatment if necessary

});

Here are some explanations:

You get the roomPushKey by reading the "data after the write" as an object: roomPushKey = afterData.inRoom. You don't need to do roomPushKey.once('child_added').then()

Once you have the roomPushKey, you create a new child node in the rooms node by using update() and creating an object with square brackets notation which allow you to assign the id of the node (i.e. roomPushKey).

Note that you could also do:

return admin.database().ref('/rooms/' + roomPushKey).set(ins);

Note also that you have to import firebase-admin in order to be able to do return admin.database().ref()...

Finally, I would suggest that you have a look at the three following videos from the Firebase team: youtube.com/watch?v=7IkUgCLr5oA&t=517s & youtube.com/watch?v=652XeeKNHSk&t=27s & youtube.com/watch?v=d9GrysWH1Lc. A must for anyone starting coding for Cloud Functions.

这篇关于如何根据 Google Cloud Functions 上初始路径的参考值将节点克隆到另一个路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持吉威生活!



[英文标题]How to clone a node to another path based on a reference value from the initial path on Google Cloud Functions?


声明:本媒体部分图片、文章来源于网络,版权归原作者所有,如有侵权,请联系QQ:330946442删除。