I have a question about Cloud Firestore V9 and using React Js. In one of my docs, there is a map. How do I access this map?

firebase screenshot

enter image description here

There’s a function named getSomethingTEST that I’m using to get the data. Then I have a ‘temporary’ button onPress={getSomethingTest}, but probably its going to be a useEffect not a button. Later I was going to set my state var “dataFB” to the tempStore array. But my issue is… I don’t know how… So I tried to console.log my results of tempStore. And I got this.. the results in console Which I believe is a nested object inside of an array. So I tried some basic things like tempStore[0].name but its of course undefined. Also I wasn’t sure how to reference a key of an object that has a space in it. For this example it was “Asem Jawa”.

    const firebaseApp = initializeApp({
    apiKey: "Hidden",
    authDomain: "Hidden",
    projectId: "Hidden",
  });

  const db = getFirestore();
  const col_p = collection(db, "WNA-Pasteur");
  const docRef = doc(db, "WNA-Pasteur", "branchData");


  const [dataFB, set_dataFB] = useState([]);

  async function getSomethingTEST() {
    const tempStore = [];
    const docSnap = await getDoc(docRef);
    tempStore.push(docSnap.data());
    console.log(tempStore);
  }

Answer

You are fetching a single document and docSnap.data() is an object containing that document’s data. You don’t have to push it an array that way.

async function getSomethingTEST() {
  const docSnap = await getDoc(docRef);
  console.log(docSnap.data().name)  
}

This should log the name but in your case, you don’t have a property name in your object so it’ll be undefined.

I wasn’t sure how to reference a key of an object that has a space in it.

You can use brackets notation:

console.log(docSnap.data()["Asem Jawa"] || "Field missing")