尝试从 Firebase 获取子记录 [英] Trying to get child records from Firebase

查看:17
本文介绍了尝试从 Firebase 获取子记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正处于 angular-fire-seed 教程阶段并尝试使用消息和子帖子,出于某种奇怪的原因,当我明确尝试显示它们时我看不到它们,但是在扩展中的父节点时可以看到它安慰.这些消息在我有 ng-repeat 的 html 中正确显示,所以我知道我至少收到了这些消息,尽管没有孩子.

Am in the angular-fire-seed tutorial stage and experimenting with messages and child posts, for some bizarre reason I cannot see the children when I explicitly try to display them, but can see it when expanding on the parent node in the console. These messages display properly in the html where I have the ng-repeat, so I know I am getting the messages at least, albeit childless.

我认为 angularfire-seed utils 可能会砍掉或切片一些孩子,所以我又回到了直接的 firebase

I thought angularfire-seed utils might be chopping or slicing some children so I reverted to straight firebase

这就是我所拥有的:

Code:
-----
var url = fbutil.ref() + "/messages/";
var ref = new Firebase(url);

var sync = $firebase(ref).$asArray();

console.log(sync);          //this I can see as a proper $firebaseArray object
console.log(sync.length);   //this displays as 0 even though length is 3 in object above
console.log(sync[1]);       //displays as undefined 

Data:
----
messages/id1/text
        /id2/text
        /id2/post
        /id3/text

预先感谢您指出我的错误假设!

Thanks in advance for pointing out what I am mis-assuming !

推荐答案

您似乎很喜欢常见的异步问题.

You seem to be falling for the common asynchonisity problem.

这是您的代码片段:

var sync = $firebase(ref).$asArray();

console.log(sync);        //this I can see as a proper $firebaseArray object
console.log(sync.length); //this displays as 0 even though length is 3 in object above
console.log(sync[1]);     //displays as undefined 

$asArray() 的调用不会立即返回 Firebase 数组,因为数据可能需要一些时间才能从 Firebase 服务器下载.因此,它返回 Firebase 数组的promise,将来会包含您的数据.

The call to $asArray() doesn't return a Firebase array immediately, since the data may take some time to come down from the Firebase servers. So instead it returns the promise of a Firebase array, something that in the future will contain your data.

当您检查控制台中的 console.log(sync) 输出时,数据将已下载,因此它(如您所说)是一个正确的同步数组.但是当 console.log(sync.length) 行运行时,数据可能还没有下载.

By the time you inspect the out of console.log(sync) in the console, the data will have downloaded so it is (as you say) a proper synchronized array. But when the console.log(sync.length) line runs, the data probably hasn't downloaded yet.

您可以使用 $loaded() 方法等待数据下载.所以:

You can wait for the data to be downloaded with the $loaded() method. So:

var sync = $firebase(ref).$asArray();

console.log(sync);
sync.$loaded().then(function(sync) {
  console.log(sync.length); 
  console.log(sync[1]);     
}

这篇关于尝试从 Firebase 获取子记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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