'Accessing nested array of documents containing collections with Firebase v9

I am trying to retrieve the resultant array of collections that has documents mapping to their respective collections in firebase v9.

For example, I have this working:

  const docRef = doc(firestoreDb, "HOUSE_PAYMENTS", 'BUILDING', "SUITE_355620", '355620');
  const docSnap = await getDoc(docRef);

  if (docSnap.exists()) {
  // this works, it outputs the data for 355620
    console.log("Document data:", docSnap.data());
  } else {
    console.log("No such document!");
  }

but I don't want to statically define the collection SUITE_355620 and document 355620, I need the entire array, so is it possible to do it like this?

  const docRef = doc(firestoreDb, "HOUSE_PAYMENTS", 'BUILDING');
  const docSnap = await getDoc(docRef);

  if (docSnap.exists()) {
    // this currently results in an empty object, can't figure out why
    console.log("Document data:", docSnap.data());
  } else {
    console.log("No such document!");
  }

at the moment I am getting an empty object. Any thoughts? Thanks :)



Solution 1:[1]

This seems to be working as expected.

If there is no document at the location referenced by docRef, the resulting document will be empty and calling exists on it will return false.

You probably have a sobcollection but no document there.

Please provide a screen-shot of your data, if this doesn't answer the query.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1