Die*_*cio 5 angularjs firebase-realtime-database google-cloud-firestore angularfire5
我有一个关于在angularfire 的firestore中插入对象的问题:
我的对象 Person.ts
name: String
age: Number
//--constructor--
//--getters and setters--
Run Code Online (Sandbox Code Playgroud)
如果我这样做,请插入:(但这是一个好习惯吗?)
[person.component.ts]
this.db.collection("person").add({
name: this.person.$nome,
age: this.person.$email
})
...
Run Code Online (Sandbox Code Playgroud)
但如果我尝试:
[person.component.ts]
this.db.collection("person").add({
Person: this.person
//or this this.person
})
Run Code Online (Sandbox Code Playgroud)
我在浏览器控制台中收到此错误:
使用无效数据调用函数 DocumentReference.set()。不支持的字段值:在 new FirestoreError (error.js:149) 处的自定义 Person 对象(在字段 Person 中找到)
Firestore 仅接受嵌入在文档中的 JavaScript 对象(如果该对象是 \xe2\x80\x9cpure\xe2\x80\x9d 对象),这意味着您在使用 TypeScript 进行编码时无法使用自定义对象。
\n\n将您的代码更改为:
\n\nthis.db.collection("person").add(Object.assign({}, this.person)); \nRun Code Online (Sandbox Code Playgroud)\n