gpt4 book ai didi

angularjs - 在firestore和goods实践中插入封装对象

转载 作者:行者123 更新时间:2023-12-02 22:33:36 24 4
gpt4 key购买 nike

我有一个关于在 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)); 

关于angularjs - 在firestore和goods实践中插入封装对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46869143/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com