Firestore:将自定义对象添加到数据库 [英] Firestore: Add Custom Object to db
本文介绍了Firestore:将自定义对象添加到数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
早上好,
我尝试从这个类中添加一个新创建的对象:
I tried adding a new created object from this class:
export class Sponsor implements ISponsor {
title: string;
description?: string;
creation: ICreation;
constructor(title: string, description: string, author: string) {
this.title = title;
this.description = description;
this.creation = new Creation(author);
}
}
在我的服务中,create 函数如下所示:
and in my service, the create function looks like:
createSponsor(sponsor) {
sponsor.id = this.afs.createId();
return this.collection.doc(sponsor.id).set(sponsor);
}
当我以这种方式尝试时,出现以下错误:
When I try it this way, I get the following error:
FirebaseError:[code=invalid-argument]:使用无效数据调用函数 DocumentReference.set().数据必须是一个对象,但它是:自定义赞助商对象
FirebaseError: [code=invalid-argument]: Function DocumentReference.set() called with invalid data. Data must be an object, but it was: a custom Sponsor object
我该如何解决这个问题?
How can I solve this issue?
推荐答案
你也可以使用 Object.assign({},sponsor)
You can also use Object.assign({}, sponsor)
所以在你的情况下会是
so in yor case it would be
this.collection.doc(sponsor.id).set(Object.assign({}, sponsor));
这篇关于Firestore:将自定义对象添加到数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文