将数据从FirebaseListObservable提取到Array [英] Extract data from FirebaseListObservable to Array

查看:106
本文介绍了将数据从FirebaseListObservable提取到Array的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对不起我的英语,我有这个可观察的:

sorry for my english, i have this observable:

this.productos = af.database.list('/productos', {
    query: {
      orderByChild: 'categoria',
      equalTo: this.catnombre
    }
    });

我需要从这里提取所有id并设置在一个数组中,但我不知道如何,谢谢。

I need extract all id from here and set in a array but i dont know how, thanks.

编辑:

我可以提取id,但我使用de key,现在我需要提取其他数据,但是snapshot.val,不工作。

I can extract the id but I use de key, now i need extract other data, but snapshot.val, dont work.

  this.productos = af.database.list('/productos/', {
    query: {
      orderByChild: 'categoria',
      equalTo: this.catnombre
    }, preserveSnapshot:true
    });

    this.productos.subscribe(snapshot => {

         snapshot.forEach(snapshot => {

           console.log(snapshot.key);
           this.idproductos.push(snapshot.key); 

         });
         console.log(this.idproductos);
    });


推荐答案

所有你需要做的就是

this.productos = af.database.list('/productos/', {
    query: {
      orderByChild: 'categoria',
      equalTo: this.catnombre
    })
  .map(products => products.map(product => product.$key));

结果将是一个可观察的密钥数组。现在你可以订阅或做任何你想做的事情。

The result will be an observable of arrays of keys. Now you can subscribe it to or do whatever else you want to.

this.productos.subscribe(keys => console.log("keys are", keys));

如果是AngularFire,以及 FirebaseListObservable 之类的东西,使用正确,你不需要担心快照,或者他们的 val(),或者做 forEach 在他们身上,或者将元素放到你自己的数组上。 FirebaseListObservable 是一个可观察的数组。简单地 map 创建其他可观察对象,正如我们上面创建的一个可观察的键数组,或订阅它以获取基础数据。

If AngularFire, and things like FirebaseListObservable, are used correctly, you don't need to worry about snapshots, or taking their val(), or doing forEach on them, or taking elements and putting them onto your own array. The FirebaseListObservable is an observable of arrays. Simply map it to create other observables, as we have done above to create an observable of arrays of keys, or subscribe to it to get the underlying data.

这篇关于将数据从FirebaseListObservable提取到Array的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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