skip to Main Content

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:

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?

2

Answers


  1. 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)) {
    
    }
    
    Login or Signup to reply.
  2. Firestore maps is an object so I think this will fit your needs:

    let map = document.data().map
    if (
      typeof map === 'object' &&
      // as typeof array === 'object' we need to check if is not an array
      !Array.isArray( map )
    ) {
      alert("Is a Map.")
    } else {
      alert("It's not a Map.")
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search