angularfire2:破坏性更新(set())和非破坏性更新(update())之间的差异

angularfire2:破坏性更新(set())和非破坏性更新(update())之间的差异,angular,firebase,angularfire2,Angular,Firebase,Angularfire2,目前正在研究此repos身份验证 此代码段将用户名和电子邮件写入实时数据库,在该repo的身份验证服务中,作者使用此方法在用户每次注册和登录时更新用户数据 //// Helpers //// private updateUserData(): void { // Writes user name and email to realtime db // useful if your app displays information about users or for a

目前正在研究此repos身份验证

此代码段将用户名和电子邮件写入实时数据库,在该repo的身份验证服务中,作者使用此方法在用户每次注册和登录时更新用户数据

  //// Helpers ////

  private updateUserData(): void {
    // Writes user name and email to realtime db
    // useful if your app displays information about users or for admin features

    const path = `users/${this.currentUserId}`; // Endpoint on firebase
    const data = {
      email: this.authState.email,
      name: this.authState.displayName
    }

    this.db.object(path).update(data)
      .catch(error => console.log(error));

  }
在我目前正在进行的项目中,我使用上面的代码片段将用户详细信息写入firebase,用户详细信息(不仅仅是电子邮件和通行证)仅在注册时与回购不同,他还使用代码片段更新登录和注册时的用户详细信息

按照

this.db.object(path).update(data)
在文档中有两种类似的方法,即
.update()
set()
,文档中说
.update()
非破坏性更新
set()
破坏性更新。我试过这两个,它和另一个一样有效


我的问题是,正如我在上面解释的,我只在注册时使用代码片段,我应该使用
set()
还是
update()
?“什么是破坏性的”和“什么是非破坏性的”更新,链接解释了这些大帮助是什么。

正如我注意到的,所有框架/工具对这两个操作使用相同的逻辑。所以
set()
销毁对象并分配新值,而
update()
只更新定义的属性

假设您在db中有object:

car 
 color:  "red",
 engine: "v6"
当您运行类似于
car.set(color=“blue”)
的程序时,对象变为:

car 
 color:  "blue",
 engine: undefined
执行
update(color=“blue”)
会得到:

car 
 color:  "blue",
 engine: "v6"
这就解释了为什么不能在原语上使用
update()
——它们只有一个属性,所以无论如何都会被重置