代码之家  ›  专栏  ›  技术社区  ›  Diego Venâncio

在firestore和商品实践中插入封装对象

  •  5
  • Diego Venâncio  · 技术社区  · 8 年前

    我有一个关于在中插入对象的问题 在里面 角火 :

    name: String
    age: Number
    //--constructor--
    //--getters and setters--
    

    如果我这样做,请插入ok:(但这是好做法吗?)

    [person.component.ts]
          this.db.collection("person").add({
                  name: this.person.$nome,
                  age: this.person.$email
              })
        ...
    

    但如果我尝试:

        [person.component.ts]
             this.db.collection("person").add({
                         Person: this.person
    //or this this.person
                      })
    

    我在浏览器控制台中遇到以下错误:

    功能文档参考。使用无效数据调用set()。不支持的字段值:自定义Person对象(在字段Person中找到) 在新FirestoreError(错误。js:149) 在

    1 回复  |  直到 8 年前
        1
  •  4
  •   Mateus Forgiarini da Silva    8 年前

    Firestore只接受嵌入文档中的JavaScript对象(如果它是纯对象),这意味着在使用TypeScript编码时不能使用自定义对象。

    将代码更改为:

    this.db.collection("person").add(Object.assign({}, this.person));