Hi guys,
thanks for your good work! Question: It麓s possible that the Firebase $key reference is not working with angularfire2? To grab informations from a selected item I need the item key (-KwnRT1lMjg78VZWwi0m for example) in an array with the item data.
So I have to build my own object:
constructor(public navCtrl: NavController, public navParams: NavParams, private dbAction: DbActionsProvider, private afDatabase: AngularFireDatabase) {
// Build Current User ID
this.currentUserID = this.dbAction.currentUserID().subscribe(data => {
this.currentUserID = data.uid;
});
}
ngOnInit() {
// Get data
this.afDatabase.object('data/users/' + this.currentUserID + '/visits')
.snapshotChanges().map(action => {
const data = action.payload.toJSON();
return data;
})
.subscribe(result => {
Object.keys(result).map(key => {
this.visits.push({ 'key': key, 'data':result[key]
});
}); console.log(this.visits)
})
}
Not a pretty solution in case of a realtime database... :/ Maybe exists a more elegant solution?
They have stopped putting the $key on the object directly, my guess is because they had to unwrap the objects before pushing downstream. You can read details here
and here's the example of how to get the key value
constructor(afDb: AngularFireDatabase) {
afDb.object('items/1').snapshotChanges().map(action => {
const $key = action.payload.key;
const data = { $key, ...action.payload.val() };
return data;
}).subscribe(item => console.log(item.$key));
}
I like this compact version of above from an example on the lists docs:
this.items = this.itemsRef.snapshotChanges().map(changes => {
return changes.map(c => ({ key: c.payload.key, ...c.payload.val() }));
Most helpful comment
They have stopped putting the
$keyon the object directly, my guess is because they had to unwrap the objects before pushing downstream. You can read details hereand here's the example of how to get the
keyvalueI like this compact version of above from an example on the lists docs: