Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to check if a field in Firestore document is a Map?

I have this document:

enter image description here

As you can see, the field map is of type Map. I read the data from Firestore using this function:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

async function getJohn() {
  var ref = doc(db, "users/john");
  const document = await getDoc(ref);
  if(document.exists()) {
    let map = document.data().map
    if (map instanceof Map) {
      alert("Is a Map.")
    } else {
      alert("It's not a Map.")
    }
  }
}

Even if the map is a Map in the document, I always get:

It’s not a Map.

How to solve this?

>Solution :

On the database side they call it a map, but once it arrives client side it is just a plain javascript object, never a javascript Map. If you want to distinguish it from other objects, maybe you could do a check like this:

if (typeof map === 'object' && map !== null && !Array.isArray(map)) {

}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading