[英]Firebase Cloud Functions - Synchronous Functions
I´m new to firebase cloud functions. 我是Firebase云功能的新手。 I want to create my index.js as following:
我想创建我的index.js如下:
exports.countFruits = functions.database.ref("fruits/{FruitID}").onWrite(event =>{
(...)
var sauces = [];
let ref = admin.database().ref("sauces");
ref.on("child_added", function(snapshot, prevChildKey){
let sauce = snapshot.val();
sauces.push(sauce.id);
});
// -> USE THE COMPLETED ARRAY FOR NEXT ANOTHER PURPOSE
});
So my Problem is, that I want to use the COMPLETED array while it is actually being filled. 所以我的问题是,我想在实际填充时使用COMPLETED数组。 As a consequence I want to structure my code synchronously so the next function is only executed when all sauce.id are in the array.
结果,我想同步构造代码,以便仅当所有sauce.id都在数组中时才执行下一个函数。
How can I code that? 我该如何编码?
Thank you! 谢谢!
When you listen to child_added
events, your handler will be called for each existing child and any time a child is added later. 当你听
child_added
事件,您的处理器将被调用为每个现有的儿童和儿童被后来添加的任何时间。 So there is no clear moment when all children have been processed. 因此,尚无明确的时机处理所有孩子。
If you want to handle all current children, you should use a once("value"
handler, like this: 如果要处理所有当前子级,则应使用
once("value"
处理程序,如下所示:
exports.countFruits = functions.database.ref("fruits/{FruitID}").onWrite(event =>{
(...)
var sauces = [];
let ref = admin.database().ref("sauces");
ref.once("value", function(snapshot){
snapshot.forEach(function(sauceSnapshot) {
let sauce = snapshot.val();
sauces.push(sauce.id);
});
// -> USE THE COMPLETED ARRAY FOR NEXT ANOTHER PURPOSE
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.