异步访问中火力地堡数组 [英] Asynchronous access to an array in Firebase

查看:170
本文介绍了异步访问中火力地堡数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我的code:

var userRef = new Firebase("https://awesome.firebaseio.com/users/");

var tokenRef = userRef.child(key+'/tokens');
tokenRef.once('value', function(snapshot){

  var userTokenSync = $firebase(tokenRef);
  var userTokens = userTokenSync.$asArray();


  console.log(userTokens);
  console.log(userTokens[0]);

  for(var i=0, len = userTokens.length; i < len; i++) {
     console.log(userTokens[i]);
  }
  console.log('done');
})

这code会从火力用户的令牌,我只是想浏览的令牌数组。

This code gets the tokens of a user from firebase, and I just want to browse the tokens array.

下面是控制台给我:

如你所见,我无法访问阵列。你有我如何能做到这一点任何想法?

As you can see, I cannot access the array. Do you have any idea of how I could do this?

先谢谢了。

推荐答案

欢迎到异步加载101。

Welcome to Asynchronous Loading 101.

  var userTokens = userTokenSync.$asArray();

  console.log(userTokens);
  console.log(userTokens[0]);

  for(var i=0, len = userTokens.length; i < len; i++) {
     console.log(userTokens[i]);
  }
  console.log('done');

该片段的第一行的启动从火力地堡加载数据。但是,加载可能需要一些时间。此外,由于浏览器不希望阻止的事情,它会继续执行该脚本。

The first line of this snippets starts loading your data from Firebase. But that loading might take some time. And since the browser doesn't want to block things, it continues executing the script.

通过浏览器执行你的的console.log(userTokens)的时间; 中的数据最有可能还没有被加载。因此,它只是打印对象到控制台作为占位符。

By the time the browser executes your console.log(userTokens); the data most likely hasn't been loaded yet. So it just prints the object to the console as a placeholder.

通过它获取到循环的时间,数据可能会或可能还没有从火力地堡加载。

By the time it gets to the for loop, the data may or may not yet have been loaded from Firebase.

在某些时候你点击旁边的箭头登录 userTokens 。到那个时候,这些数据是从火力地堡加载和控制台显示的最新数据。

At some point you clicked the arrow next to the logged userTokens. By that time the data had loaded from Firebase and the console shows the latest data.

该解决方案由AngularFire提供,在承诺的形式。 AngularFire承诺在未来某个时候对你的数据。如果你有一块code的加载数据时,应该只执行,你可以写这样的:

The solution is provided by AngularFire, in the form of a promise. AngularFire promises to at some point in the future have the data for you. If you have a piece of code that should only execute when the data is loaded, you can write it like this:

  var userTokens = userTokenSync.$asArray();

  console.log(userTokens);
  console.log(userTokens[0]);

  userTokens.$loaded(function(userTokens) {
    for(var i=0, len = userTokens.length; i < len; i++) {
      console.log(userTokens[i]);
    }
    console.log('done');
  });

  console.log('at last line, but not done yet');

更新

请注意,上面是何时使用 $加载一个坏榜样。由于 $加载将触发一次,它只会记录的初始的数组的内容。如果你只是想看看,如果/当你的数据加载时,惯用的方法是添加 userTokens 的范围:

Update

Note that the above is a bad example of when to use $loaded. Since $loaded will fire only once, it will only log the initial contents of the array. If you're just trying to see if/when your data has loaded, the idiomatic approach is to add the userTokens to the scope:

$scope.userTokens = userTokens;

和添加到您的视图/ HTML:

And add this to your view/HTML:

<pre>{{ userTokens | json }}</pre>

AngularFire现在将监视时userTokens加载或以任何方式修改,并会告诉AngularJS如果出现这种情况,以更新视图。

AngularFire will now monitor when the userTokens are loaded or modified in any way and will tell AngularJS to update the view if that happens.

也看到这些:

  • Firebase, AngularJS and GeoFire - wait for response from factory
  • Firebase angularfire child.$asObject give properties undefined
  • angularfire - why can't I loop over array returned by $asArray?
  • Passing variable in parent scope to callback function
  • Trying to get child records from Firebase

烨...我们得到这个问题很多。

Yup... we get this question a lot.

这篇关于异步访问中火力地堡数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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