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

angularfire2: Difference between destructive updates ( set() ) and non destructive updates ( update() )

目前正在研究https://github.com/codediodeio/angular-firestarter这个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)

我的文档 https://github.com/angular/angularfire2/blob/master/docs/2-retrieving-data-as-objects.md#api-summary 有两种相似的方法,这个 .update()set(),它在文档中说 .update()non -破坏性更新set() 破坏性更新。我尝试了这两个,它的工作原理与另一个相同,

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

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

因此,假设您在数据库中有对象:

car 
 color:  "red",
 engine: "v6"

当您 运行 类似 car.set(color = "blue") 时,对象变为:

car 
 color:  "blue",
 engine: undefined

update(color = "blue") 得到:

car 
 color:  "blue",
 engine: "v6"

这就解释了为什么你不能在基元上使用 update() - 它们只有一个 属性,所以它无论如何都会被重置。