抱歉我的英语不好,我有这个观察结果:
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/