早上好
我试着从这个类中添加一个新创建的对象:
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);
}
}
在我的服务中,创建函数如下所示:
createSponsor(sponsor) {
sponsor.id = this.afs.createId();
return this.collection.doc(sponsor.id).set(sponsor);
}
当我以这种方式尝试时,出现以下错误:
FirebaseError: [code=invalid-argument]: Function DocumentReference.set() called with invalid data. Data must be an object, but it was: a custom Sponsor object
我该如何解决这个问题?
最佳答案
你也可以使用 Object.assign({}, sponsor)
所以在你的情况下会是
this.collection.doc(sponsor.id).set(Object.assign({}, sponsor));
关于angular - Firestore : Add Custom Object to db,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46578701/