0

I am currently doing some Angular Ionic project with Firestore and stuck with one problem. Take following code as an example:

handler: data => {
     firebase.firestore().collection("categories").doc(`${data.name}`).get()
     .then((ds) => {
     if (!ds.exists){
         console.log("No such category");
     }
     else{ 
         let data = ds.data();
         let interested : Map<string, boolean> = data["interested"];
         console.log(Array.from(interested.keys());
         console.log(interested);
     }
});

In my database I have following field: data

Upon running the code, I get following error:

Error: Uncaught (in promise): TypeError: interested.keys is not a function. (In 'interested.keys()', 'interested.keys' is undefined)

When I checked the type of "interested", it returned object while I expected it to be Map. Actually, none of the Map methods work on that variable. I want to use such methods, however, I do not know how to make them work. Why it does not work and what should I do? May be I have to convert data Firestore returns into JSON object?

orimdominic
  • 995
  • 10
  • 23
arnihere
  • 11
  • 4
  • There's nothing different about an object returned from data() than any other plain JavaScript object. And TypeScript's Map is the same as ES6 map, except it adds generics. Hence, the duplicate. – Doug Stevenson Jan 19 '20 at 02:40

1 Answers1

1

Although I haven't ever worked with Maps, after seeing nils's answer on "How to convert a plain object into an ES6 Map", I believe you can do this:

const safeObject = data['interested'] || {}; // because it might be undefined
let interested: Map<string, boolean> = new Map(Object.entries(safeObject));
Stratubas
  • 2,939
  • 1
  • 13
  • 18