我使用节点Bigquery Package来运行一个简单的作业。查看作业的结果(比如data
),effective_date
属性如下所示:
effective_date: BigQueryDate { value: '2015-10-02' }
这显然是返回的data
对象中的一个对象。
将返回的json导入Firestore会出现以下错误:
UnhandledPromiseRejectionWarning: Error: Argument "data" is not a
valid Document. Couldn't serialize object of type "BigQueryDate".
Firestore doesn't support JavaScript objects with custom prototypes
(i.e. objects that were created via the 'new' operator).
有没有一种优雅的方法来处理这个问题?是否需要迭代结果并转换/删除所有对象?
5条答案
按热度按时间ltskdhd11#
firestore Node.js客户端不支持自定义类的序列化。
您将在本期中找到更多解释:
https://github.com/googleapis/nodejs-firestore/issues/143
“我们明确决定不支持Web和Node.JS客户端的自定义类的序列化”
一个解决方案是将嵌套对象转换为普通对象。例如使用lodash或JSON.stringify。
这里有一个相关的帖子:
Firestore: Add Custom Object to db
jv2fixgn2#
另一种方法是消耗更少的资源:
注意:它只对没有嵌套对象的对象起作用。
您也可以使用
class-transformer
和它的classToPlain()
沿着exposeUnsetFields
option来省略undefined
值。drnojrws3#
如果你有一个
FirebaseFirestore.Timestamp
对象,那么不要使用JSON.parse(JSON.stringify(obj))
或classToPlain(obj)
,因为它们会在存储到Firestore时损坏它。最好使用
{...obj}
方法。注意:不要对document类中的任何嵌套对象使用
new
操作符,它将不起作用。相反,为嵌套对象属性创建interface
或type
,如下所示:如果你真的想存储一个由嵌套更深的类对象组成的类对象,那么使用这个函数首先将它转换为普通对象,同时保留
FirebaseFirestore.Timestamp
方法。rjzwgtxy4#
将值序列化为有效的Firestore Document数据,包括
object
及其查尔兹和Array
及其项使用方法:
hwazgwia5#
我在Firestore中将一个模块转换为一个类时遇到了这个问题,问题是我以前使用的是一个管理firestore示例,并引用了
@google-cloud
中的一些字段信息,而不是使用firebase管理示例中的方法。应该使用admin包中的引用: