从 FirebaseListObservable 中提取数据到数组 [英] Extract data from FirebaseListObservable to 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 中提取数据到数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!