我是否正确使用ForEach? [英] Am I using ForEach correctly?

查看:38
本文介绍了我是否正确使用ForEach?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下布局在Firebase中构建类似于状态的系统:

I'm working on a presence-like system in firebase with following layout:

firebase {
   user1 {
     isOnline: true
   }
   user 2 {
     isOnline: true
   }
   user3 {
     isOnline: false
   }
}

isOnline布尔值是我稍后将用于将在线用户的名称输出到控制台的方法

The isOnline booleans are what I am going to use later to output the names of the users that are online to the console

例如,在上面的例子中,它会说:

So for example, in the case above it would say:

user1 is online.
user2 is online.

这是我的代码:

var gameRef = new Firebase("https://xxx.firebaseio.com/");
var userOnline = new Firebase('https://xxx/.info/connected');

        userOnline.on('value', function (snapshot) {
          if (snapshot.val()) {
               gameRef.child(user).update({
                    isOnline : true
                });
          }
          else {
              gameRef.child(user).update({
                    isOnline : false
                });
          }
        });

       // for each user that is online, output to the console
       gameRef.forEach(function (snapshot) {
            var obj = snapshot.val();
            if(obj.isOnline == true) {
                console.log(obj.name + " is online.");
            }
        }); 

forEach似乎有问题,我该如何解决? 谢谢.

There seems to be a problem with my forEach, how can I fix this? Thanks.

推荐答案

您不能forEach在引用上,而只能在快照上.

You cannot forEach over a ref, but only over a snapshot.

   // for each user that is online, output to the console
   gameRef.on('value', function(function(gamesSnapshot) {
       gamesSnapshot.forEach(function (snapshot) {
           var obj = snapshot.val();
           if(obj.isOnline == true) {
               console.log(obj.name + " is online.");
           }
       }
   }); 

此代码具有两个快照变量:

This code has two snapshot variables:

  • gameSnapshot是父节点中的数据
  • snapshot是特定玩家的数据
  • gameSnapshot is the data in the parent node
  • snapshot is the data of a specific player

以上方法将下载所有播放器,即使您只想与在线播放器打交道.在这种情况下,查询Firebase效率更高,以便只返回在线玩家.

The approach above will download all players, even though you are only looking to deal with players that are online. It is more efficient in this case, to query Firebase so that it only returns players that are online.

   // for each user that is online, output to the console
   var onlinePlayers = gameRef.orderByChild('isOnline').equalTo(true);
   onlinePlayers.on('child_added', function(function(snapshot) {
       var obj = snapshot.val();
       if(obj.isOnline == true) {
           console.log(obj.name + " is online.");
       }
   }); 

代码现在侦听child_added事件,因为Firebase一次向我们喂食玩家一个人.将播放器映射到HTML元素后,您可能还必须处理child_changedchild_removed.

The code now listens for the child_added event, since Firebase spoon-feeds us the players one at a time. You will probably also have to handle child_changed and child_removed, once you map the players to HTML elements.

即使这将导致更多的代码,但我通常建议使用查询和child_*事件,因为它们会限制Firebase最初和在发送时(例如,发送给您)的数据.玩家离线.

Even though this will result in a bit more code, I would normally recommend using querying and the child_* events, since they limit the data that Firebase sends you both initially and when e.g. a player goes offline.

这篇关于我是否正确使用ForEach?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆