如何在firebase中为所有孩子使用有条件的条件

发布于 2025-02-12 14:04:48 字数 413 浏览 0 评论 0原文

下面的代码是我一直在尝试执行的操作每个孩子都会立即进行火,然后执行检查。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

自此以后,行同陌路 2025-02-19 14:04:48

child_added为每个孩子打电话给处理程序,其中没有办法知道这是否是最后一个孩子。为此,您需要收听value事件。

因此,一个选择是:

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

dbRef.on("value", (snapshot) => {
    if (found) {
         console.log("Finished!");
    }else{
         console.log("Nope!");
    }
});

这很棒,尽管将数据传递给了两个听众,但Firebase可确保仅下载数据一次。


如果您愿意,您也只能使用value侦听器进行相同的操作:

let dbRef = firebase.database().ref("/ExamplePath");

dbRef.on("value", (snapshot) => {
    let found = false;
    snapshot.forEach((child) => {
         if (child.val().test == testingVal) {
              console.log("Found One!");
              found = true;
         }
    });

    if (found) {
         console.log("Finished!");
    }else{
         console.log("Nope!");
    }
});

两种方法的结果和效率都是相同的,因此您应该选择最方便的应用程序。例如,当我负责直接更新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 the value event.

So one options is:

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

dbRef.on("value", (snapshot) => {
    if (found) {
         console.log("Finished!");
    }else{
         console.log("Nope!");
    }
});

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 valuelistener:

let dbRef = firebase.database().ref("/ExamplePath");

dbRef.on("value", (snapshot) => {
    let found = false;
    snapshot.forEach((child) => {
         if (child.val().test == testingVal) {
              console.log("Found One!");
              found = true;
         }
    });

    if (found) {
         console.log("Finished!");
    }else{
         console.log("Nope!");
    }
});

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 the child_* event tells me pretty directly what to do.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文