【问题标题】:angularfire2: Difference between destructive updates ( set() ) and non destructive updates ( update() )angularfire2:破坏性更新( set() )和非破坏性更新( update() )之间的区别
【发布时间】:2017-09-27 15:12:05
【问题描述】:

目前正在研究https://github.com/codediodeio/angular-firestarter这个repos认证。

这个sn-p将用户名和email写入实时数据库,在这个repo的Authentication服务中,作者使用这个方法在用户每次注册和登录时更新用户数据。

  //// 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));

  }

在我目前正在进行的副项目中,我使用上面的 sn-p 将用户详细信息写入 firebase,仅在注册时用户详细信息(不仅是电子邮件和通行证)与他在 repo 中使用 sn- p 还可以在登录和注册时更新用户详细信息。

在一行

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()非破坏性更新set() 破坏性更新。我试过这两个,它的工作原理和另一个一样,

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

【问题讨论】:

    标签: angular firebase angularfire2


    【解决方案1】:

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

    所以,假设你在 db 中有对象:

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

    当你运行类似car.set(color = "blue") 的东西时,对象变成:

    car 
     color:  "blue",
     engine: undefined
    

    并且做update(color = "blue") 给出:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-03-24
      • 1970-01-01
      • 1970-01-01
      • 2019-01-22
      • 2018-10-23
      • 2016-02-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多