使用angulafire可以在将其保存到数据库之前检索记录pushid:
myModelDto.key = dbRef.push().key; // Add the myModelDto to the relative colletion
这很方便,因为我可以将firebase密钥存储为我的模型的属性.现在使用angularfire2,这似乎不可能以干净/简单的方式:
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) });
因此,我想知道我是否以错误的方式接近firebase(为了方便起见,希望将一个关键属性绑定到我的模型上),或者是否有更好的方法将pushid添加到新添加的记录中.
解决方法
看一下angularfire2问题的答案
https://github.com/angular/angularfire2/issues/199
kanafghan写道:
const pushId = this.afDb.createPushId(); const item = { ...item,id: pushId }; this.afDb.list('items').set(item.id,item);