[英]How do I write/update data to multiple child nodes via Firebase Cloud functions?
I've got a database that looks like this: 我有一个看起来像这样的数据库:
I want to reset the Patient_List
and Current_Token
children to 0 at a particular time (say 12 AM) automatically for all the users in the database. 我想在特定时间(例如12 AM)为数据库中的所有用户自动将Patient_List
和Current_Token
子级重置为0。
I went through the Firebase cloud function docs and these projects on gitHub: Deleting nodes and Delete unused user accounts using Cron . 我在gitHub上浏览了Firebase云功能文档和这些项目: 使用Cron 删除节点并删除未使用的用户帐户 。 Both of the above examples update and change data based on a particular UID ie update only particular nodes based on the UID passed. 以上两个示例均基于特定的UID更新和更改数据,即基于传递的UID仅更新特定的节点。 I would like to know if it is possible to update values for the above child elements in all the nodes and if so , how would it be done? 我想知道是否可以在所有节点中更新上述子元素的值,如果可以,将如何完成?
It's not possible to have a single update statement affect a child of all database nodes under some location. 一个更新语句不可能影响某个位置下所有数据库节点的子级。 You will have to iterate all the nodes (by querying/reading them somehow), then update each one that you find. 您将必须迭代所有节点(通过某种方式查询/读取),然后更新找到的每个节点。 This is potentially a costly operation in terns of bandwidth. 这可能是花费大量带宽的昂贵操作。
Only use the general ref, and before that reference all nodes that you want to update, just like this: 仅使用常规引用,并在引用之前要更新的所有节点,如下所示:
var gralRef = firebase.database().ref();
var obj_update={};
obj_update[`users/${firstuser}/name`]='John';
obj_update[`users/${seconduser}/name`]='Mark';
obj_update[`users/${seconduser}/address`]='New York';
gralRef.update(obj_update);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.