With angulafire
it was possible to retrieve a record pushid before saving it to the database:
myModelDto.key = dbRef.push().key;
// Add the myModelDto to the relative colletion
This was handy, as I could store the firebase key as property of my model.
Now with angularfire2
this does not seem possible in a clean/simple way:
constructor(private angFire: AngularFire) {
this.placeRef$ = angFire.database.list('/places');
}
insertPlace = (place: IPlace): firebase.Thenable<IPlace> => {
return this.placeRef$.push(place)
.then(item => {
place.id = item.key;
this.placeRef$.update(item.key, place)
});
Therefore I am wondering whether I am approaching firebase in a wrong way (wishing to have a key property bound to my model for convenience) or if there is a better way to add the pushid to newly added records.
Take a look at this answer on angularfire2's issues
https://github.com/angular/angularfire2/issues/199
kanafghan writes:
const pushId = this.afDb.createPushId();
const item = { ...item, id: pushId };
this.afDb.list('items').set(item.id, item);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With