如何在firebase中为所有孩子使用有条件的条件
下面的代码是我一直在尝试执行的操作每个孩子都会立即进行火,然后执行检查。
let dbRef = firebase.database().ref("/ExamplePath");
let found = false;
dbRef.on("child_added", (child) => {
if (child.val().test == testingVal) {
console.log("Found One!");
found = true;
}
});
if (found) {
console.log("Finished!");
}else{
console.log("Nope!");
}
The code below is what I have been trying to do but the dbRef.on("child_added...
executes after the ending check. I know why this is happening so how could I do a similar conditional with every child in a firebase path at once and then execute a check afterward. Thanks.
let dbRef = firebase.database().ref("/ExamplePath");
let found = false;
dbRef.on("child_added", (child) => {
if (child.val().test == testingVal) {
console.log("Found One!");
found = true;
}
});
if (found) {
console.log("Finished!");
}else{
console.log("Nope!");
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
child_added
为每个孩子打电话给处理程序,其中没有办法知道这是否是最后一个孩子。为此,您需要收听value
事件。因此,一个选择是:
这很棒,尽管将数据传递给了两个听众,但Firebase可确保仅下载数据一次。
如果您愿意,您也只能使用
value
侦听器进行相同的操作:两种方法的结果和效率都是相同的,因此您应该选择最方便的应用程序。例如,当我负责直接更新UI中的每个元素时,我更喜欢听
儿童_
事件,因为child _*
事件事件可以直接告诉我该怎么做。The
child_added
handler is called for each child, and there is no way inside it to know whether this was the last child. For that you'll want to listen to thevalue
event.So one options is:
This works great, and Firebase ensures the data gets only downloaded once, despite it being passed to both listeners.
If you prefer you can also do the same with just a
value
listener:The result and efficiency of both approaches is the same, so you should choose whatever is most convenient for your app. For example, I prefer listening to
child_
events when I'm responsible for updating each element in the UI directly, as thechild_*
event tells me pretty directly what to do.