0

I am trying to set data in a Firestore document from an object of a TypeScript class -

class Quest {

    id: number = Date.now();

    index: number = 0;

    quest: string[];
    options = new Map<string, string[]>();  

    goals: string[];
    
}

To convert the class to JSON -

questDocRef.set(JSON.parse(JSON.stringify(quest)));

This sets all the fields in the quest doc except the Map field named options.

What would be a good way to achieve this?

1
  • JSON does not support ES2015's Map, Set, WeakMap, and WeakSet. It supports two types of aggregations: objects and arrays. Commented Sep 9, 2020 at 21:06

1 Answer 1

0

Firestore does not understand JavaScript ES6 Map type objects. It only understands the native types used with JSON: null, string, number, boolean, object, array.

Instead of a Map, consider using an object. Simply populate it with the fields and values you want to store in the document, and that will become a map type field in the document.

class Quest {

    id: number = Date.now();

    index: number = 0;

    quest: string[];
    options: { [key: string]: string[] } = {};

    goals: string[];
    
}

The type shown here will require that all options object keys are strings and all values are string arrays.

Not the answer you're looking for? Browse other questions tagged or ask your own question.