How do I write/update data to multiple child nodes via Firebase Cloud functions?

ε祈祈猫儿з 提交于 2020-01-25 08:17:26

问题


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.

I went through the Firebase cloud function docs and these projects on gitHub: Deleting nodes and Delete unused user accounts using Cron. Both of the above examples update and change data based on a particular UID i.e update only particular nodes based on the UID passed. 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?


回答1:


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.




回答2:


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


来源:https://stackoverflow.com/questions/53024212/how-do-i-write-update-data-to-multiple-child-nodes-via-firebase-cloud-functions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!