Reputation: 1
"items": {
"a" : {
"size" : "small",
"text" : "small thing"
},
"b" : {
"size" : "medium",
"text" : "medium sample"
},
"c" : {
"size" : "large",
"text" : "large widget"
}
}
Suppose, I have data as above. I want to get data of key a from items list in component file without iterating over whole list. I tried from the docs. But could not find this kind of requirement. Solutions on this platform are related to previous versions before 5.0. Does anyone know how can we achieve it?
Upvotes: 0
Views: 1331
Reputation: 309
I suppose you are referring to Real Time DB because you mentioned "items list", where in FireStore it is collection.
To get an object, use the AngularFireDatabase.object() function.
Examples:
// Get an object, just the data (valuesChanges function), only once (take(1) function)
import 'rxjs/add/operator/take';
let subscribe = AngularFireDatabase.object('items/a').valueChanges().take(1).subscribe(
data =>{
console.log(data);
}
);
Another way is to call the subscrible.unsubscribe() function inside the subscrible return instead of using the take() function:
let subscribe = AngularFireDatabase.object('items/a').valueChanges().subscribe(
data =>{
console.log(data);
subscribe.unsubscribe();
}
);
The above example get the data at the time of execution, not getting changes that occur with the object after that.
// Get an object, just the data (valuesChanges function), and all subsequent changes
let subscribe = AngularFireDatabase.object('items/a').valueChanges().subscribe(
data =>{
console.log(data);
}
);
The above example get the data every time there are changes in the object.
I have tested both examples.
You can read about AnglarFire2 version 5, here and here.
You can read about retrieve data from objects here.
Upvotes: 1