加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

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

发布时间:2020-12-17 18:06:07 所属栏目:安全 来源:网络整理
导读:使用angulafire可以在将其保存到数据库之前检索记录pushid: myModelDto.key = dbRef.push().key; // Add the myModelDto to the relative colletion 这很方便,因为我可以将firebase密钥存储为我的模型的属性.现在使用angularfire2,这似乎不可能以干净/简单
使用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);

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读