我正在尝试从TypeScript类的对象在Fi恢复文档中设置数据-
class Quest {
id: number = Date.now();
index: number = 0;
quest: string[];
options = new Map<string, string[]>();
goals: string[];
}
将类转换为JSON-
questDocRef.set(JSON.parse(JSON.stringify(quest)));
这将设置任务文档中除名为选项的地图字段之外的所有字段。
实现这一目标的好方法是什么?
Firestore 不理解 JavaScript ES6 Map 类型的对象。它只理解与 JSON 一起使用的本机类型:null、string、number、boolean、object、array。
不要使用地图,考虑使用一个对象。只需用想要存储在文档中的字段和值填充它,它将成为文档中的地图类型字段。
class Quest {
id: number = Date.now();
index: number = 0;
quest: string[];
options: { [key: string]: string[] } = {};
goals: string[];
}
这里显示的类型要求所有<code>选项</code>对象键都是字符串,所有值都是字符串数组。