在 firestore 和良好实践中插入封装对象
Inserting encapsuled object in firestore and goods practices
我有一个关于在 angularfire 中的 firestore 中插入对象的问题:
我的对象Person.ts
name: String
age: Number
//--constructor--
//--getters and setters--
如果我这样做,插入确定:(但这是好习惯吗?)
[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
})
我在浏览器控制台中收到此错误:
函数 DocumentReference.set() 使用无效数据调用。不支持的字段值:自定义 Person 对象(在字段 Person 中找到)
在新的 FirestoreError (error.js:149)
在
Firestore 只接受文档中嵌入的 JavaScript 对象,如果它是“纯”对象,这意味着您不能在使用 TypeScript 编码时使用自定义对象。
将您的代码更改为:
this.db.collection("person").add(Object.assign({}, this.person));
我有一个关于在 angularfire 中的 firestore 中插入对象的问题:
我的对象Person.ts
name: String
age: Number
//--constructor--
//--getters and setters--
如果我这样做,插入确定:(但这是好习惯吗?)
[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
})
我在浏览器控制台中收到此错误:
函数 DocumentReference.set() 使用无效数据调用。不支持的字段值:自定义 Person 对象(在字段 Person 中找到) 在新的 FirestoreError (error.js:149) 在
Firestore 只接受文档中嵌入的 JavaScript 对象,如果它是“纯”对象,这意味着您不能在使用 TypeScript 编码时使用自定义对象。
将您的代码更改为:
this.db.collection("person").add(Object.assign({}, this.person));