使用 angulafire
可以在将记录保存到数据库之前检索记录 pushid:
myModelDto.key = dbRef.push().key;
// Add the myModelDto to the relative colletion
这很方便,因为我可以将 firebase key 存储为模型的属性。
现在使用 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(为了方便起见希望将一个关键属性绑定(bind)到我的模型)或者是否有更好的方法将 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);
关于angularfire2 在保存模型对象之前获取新的 pushid,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43546002/