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?

Aluan Haddad
  • 29,886
  • 8
  • 72
  • 84
  • JSON does not support ES2015's `Map`, `Set`, `WeakMap`, and `WeakSet`. It supports two types of aggregations: objects and arrays. – Aluan Haddad Sep 09 '20 at 21:06

1 Answers1

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.

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441