angularfire2 在保存模型对象之前获取新的 pushid

标签 angular firebase angularfire2

使用 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/

相关文章:

javascript - Angular/Firebase : How to put user response in front-end model

javascript - 是否可以在 Ionic2/Angular2 应用程序中使用 HTTPS/SSL?

Angular 2 路由无法正常工作

Firebase 3.0 + Ember 2.0 : The Torii adapter must implement `open` for a session to be opened

javascript - 如何在不删除其他内容的情况下更新数据?

angular - map 运算符不适用于 AngularFireAuth Observable

angular - 特征切片为空时的 NgRx 选择器

angular - 无法绑定(bind)到 'matRowDefColumns',因为它不是 'mat-row' 的已知属性

android - 仅向 firebase 中的一个用户发送通知

angular - 如何使用 snapshotChanges() 方法获取键值和过滤数据?