gpt4 book ai didi

angular - Firestore : Add Custom Object to db

转载 作者:太空狗 更新时间:2023-10-29 17:00:26 24 4
gpt4 key购买 nike

早上好

我试着从这个类中添加一个新创建的对象:

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/

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