使用Firebase返回数组 [英] Returning an Array using Firebase

查看:96
本文介绍了使用Firebase返回数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

试图找到使用Q库(或任何类似的库,我不是部分)在 Node.js 中返回数据数组的最好的例子使用Firebase .on(child_added);



我试过使用 Q .all(),但它似乎永远不会等待承诺之前填写返回。这是我现在的例子:

$ p $ function getIndex()
{
var deferred = q.defer( );

deferred.resolve(new FirebaseIndex(Firebase.child('users')。child(user.app_user_id).child('posts'),Firebase.child('posts')));

return deferred.promise;


函数getPost(后)
{
var deferred = q.defer();
deferred.resolve(post.val());
return deferred.promise;
}

函数getPosts()
{
var promises = [];
$ b $ getIndex()。then(function(posts){
posts.on('child_added',function(_post){
promises.push(getPost(_post));
});
});
return q.all(promises);

$ / code $ / $ p

解决方案

getPosts )。它在一个异步函数中将一个promise放到你的数组中 - 这是行不通的,因为在promise对象被添加之前调用了q.all。

另外,child_added是一个实时的事件通知。因为没有全部这样的东西,所以不能用它来获取所有的数据。数据在实时环境中不断变化。 FirebaseIndex也在内部使用了child_added回调函数,所以这个函数也不能用于这个用例。

你可以使用'value'回调函数来获取所有文章不是特定的记录子集):

  function getPosts(){
var def = q.defer( );
Firebase.child('users')。once('value',function(snap){
var records = [];
snap.forEach(function(ss){
records.push(ss.val());
});
def.resolve(records);
});
返回def.promise;



$ b $ p
$ b现在是时候考虑实时环境。最有可能的是,在开始工作之前,所有数据都不需要存在。

考虑一下每个记录,并将它们附加到任何DOM或数组,他们需要存储,并从一个事件驱动模型,而不是一个GET / POST中心的方法工作。

幸运的是,你可以完全绕过这个用例。


Trying to find the best-use example of returning an array of data in Node.js with Q library (or any similar library, I'm not partial) when using Firebase .on("child_added");

I've tried using Q.all() but it never seems to wait for the promises to fill before returning. This is my current example:

function getIndex()
{
    var deferred = q.defer();

    deferred.resolve(new FirebaseIndex( Firebase.child('users').child(user.app_user_id).child('posts'), Firebase.child('posts') ) );

    return deferred.promise;
}

function getPost( post )
{
    var deferred = q.defer();
    deferred.resolve(post.val());
    return deferred.promise;
}

function getPosts()
{
    var promises = [];          

    getIndex().then( function (posts) {
        posts.on( 'child_added', function (_post) {
            promises.push( getPost(_post) );
        });
    });
    return q.all(promises);
}

解决方案

The problem occurs in getPosts(). It pushes a promise into your array inside an async function--that won't work since q.all is called before the promise objects have been added.

Also, child_added is a real-time event notification. You can't use that as a way to grab "all of the data" because there is no such thing as "all"; the data is constantly changing in real-time environments. FirebaseIndex is also using child_added callbacks internally, so that's not going to work with this use case either.

You can grab all of the posts using the 'value' callback (but not a specific subset of records) as follows:

function getPosts() {
   var def = q.defer();
   Firebase.child('users').once('value', function(snap) {
      var records = [];
      snap.forEach(function(ss) {
         records.push( ss.val() );
      });
      def.resolve(records);
   });
   return def.promise;
}

But at this point, it's time to consider things in terms of real-time environments. Most likely, there is no reason "all" data needs to be present before getting to work.

Consider just grabbing each record as they come in and appending them to whatever DOM or Array where they need to be stored, and working from an event driven model instead of a GET/POST centered approach.

With luck, you can bypass this use case entirely.

这篇关于使用Firebase返回数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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