Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

angularfire2 gets new pushid before saving model object

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.

like image 532
Francesco Avatar asked Nov 08 '22 23:11

Francesco


1 Answers

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);
like image 141
cgiacomi Avatar answered Nov 15 '22 10:11

cgiacomi