Using Firebase and Angular 5, I am using the snapshot method to retrieve data without listening for changes. I want to keep each item's index/key as it is being used for a link to navigate to a page for the clicked item.
Here is the TS:
features = [];
notfeatures = [];
constructor(private Svc: Service, private http: HttpClient){
firebase.database().ref('/reviews').once('value').then((snapshot)=> {
console.log("snapshot", (snapshot.val()));
..........
Logging (snapshot.val())
gives me an object (that I can't iterate over) of:
-L6ZwjBbyi4jz21OEoST: {category: "Movies", contributor: "CF1", feature: "No", …}
randomkey1: {category: "Movies", contributor: "CF1", feature: "No", …}
randomkey2: {category: "Movies", contributor: "DS1", feature: "Yes", …}
randomkey3: {category: "TV", contributor: "KH1", feature: "No", …}
So, I used forEach
to get all items one by one and push them to two different arrays if a condition is met.
..........
snapshot.forEach(snapshot => {
console.log(snapshot.key, snapshot.val());
if (snapshot.val().feature == "Yes") {
this.features.push(snapshot.val())
console.log("feature", this.features)
}
if (snapshot.val().feature == "No") {
this.notfeatures.push(snapshot.val())
console.log("notfeature", this.notfeatures)
}
});
})
}
However, doing this means that I lose snapshot.key
, which I need for navigation.
How can I keep each item's snapshot.key
for navigation / data purposes?