首页 > 解决方案 > Firestore 使用 Angular Map 对象设置文档

问题描述

我想为我的 firstore 设置一个文档。该文档如下所示:

export interface ICountry {
  key: string;
  translations: Map<string,string>;
}

例如,国家“法国”将作为翻译

translations.get('fr') === 'France'; // French
translations.get('nl') === 'Frankrijk'; // Dutch
translations.get('de') === 'Frankreich'; // German

但如果我现在想将该文件上传到我的 Firestore,

public async setCountry(country: ICountry): Promise<void> {
  return this.firestore
  .collection(this.collectionPath)
  .doc(country.key).set(country);
}

然后我收到以下错误

错误:未捕获(承诺中):FirebaseError:[code=invalid-argument]:使用无效数据调用的函数 DocumentReference.set()。不支持的字段值:自定义 Map 对象(在文档国家/法国的字段翻译中找到)

Firestore 似乎无法保存这种Map格式。所以我现在想知道

  1. 是否有更好的数据结构来保存我的翻译?
  2. 如果没有,我需要将我的转换Map<string,string>为什么以及如何保存它。

我希望最终文件看起来像这样 在此处输入图像描述

标签: angulargoogle-cloud-firestore

解决方案


推荐阅读