firebase - 将数据从 FirebaseListObservable 提取到数组

标签 firebase ionic-framework firebase-realtime-database ionic2 angularfire2

抱歉我的英语不好,我有这个观察结果:

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

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

编辑:

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

  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));

结果将是键数组的可观察值。现在您可以订阅它或做任何您想做的事情。

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

如果 AngularFire 以及 FirebaseListObservable 使用正确,您无需担心快照、获取其 val() 或执行 forEach 它们,或者获取元素并将它们放入您自己的数组中。 FirebaseListObservable 是数组的可观察对象。只需映射它即可创建其他可观察量,就像我们上面创建键数组的可观察量一样,或者订阅它以获取底层数据。

关于firebase - 将数据从 FirebaseListObservable 提取到数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43879231/

相关文章:

firebase - 在onPressed操作期间未在Flutter登录表单中找到路由

ruby - Cocoapods 安装错误 mkmf.rb 找不到 ruby​​ 的头文件

angular - ionic 3 : Menutoggle keeps getting hidden

java - Android Studio如何从Firebase的推送中获取id

python - Firestore错误消息: Operator string 'in' is invalid

firebase - 错误 : No named parameter with the name 'merge'

ionic-framework - 访问pwa地址的url参数

android - 错误 : ANDROID_HOME is not set and "android" command not in your PATH. 您必须至少满足以下条件之一

firebase - 具有一个 Firebase 身份验证的多个 Firebase 数据库

ios - 循环快照 .values 是随机的吗?