Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

334
Vistas
Fetching documents of sub-collections in firestore

I'm trying to fetch sub-collection documents from my firestore database.

Collection Imgs: enter image description here enter image description here enter image description here

My current code:

const fetchHighlight =async()=>{
      
    const Highlight = []
    const HighlightDbId = await 
 db.collection('highlights').doc('2SCS2S0JnzngWEiYkHNk').collection('4C4kd2QnaQhcp9knexkW').get()
       console.log(HighlightDbId)
    }
    React.useEffect(()=>
    {
      fetchHighlight ()
     
    }, [])
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

You forgot to fetch your query at the end of your chain, and the subcollection 4C4kd2QnaQhcp9knexkW does not exist, it's the id of a document in the subcollection you're trying to access. The right subcollection ID was hBYWvZ3KN3NLLrucTpryETQZnz2.
To sum up, you could go this way:

const yourDocument = (await db.collection('highlights').doc('2SCS2S0JnzngWEiYkHNk')
  .collection('hBYWvZ3KN3NLLrucTpryETQZnz2').doc('YOUR_DOC_ID').get()).data()

or this way:

const yourDocument = (await db.collection('highlights/2SCS2S0JnzngWEiYkHNk/hBYWvZ3KN3NLLrucTpryETQZnz2')
  .doc('YOUR_DOC_ID').get()).data()

Edit If you want to fetch only the first document of the subcollection you can go this way:


const yourDocument = (await db.collection('highlights').doc('2SCS2S0JnzngWEiYkHNk')
  .collection('hBYWvZ3KN3NLLrucTpryETQZnz2') // subcollection ref
  .orderBy("createdAt", "asc") // index
  .limit(1) // limit the size of your response
  .get()) // send the request and wait for it (you could also use '.then()' here)
  .docs[0] // get the first doc of the array
  .data() // retrieve the doc's data use `.id` instead if you want its id

And if you want to get the first subcollection of a doc you should go this way with the listCollections method:

const subcollectionId = (await db
  .doc('highlights/2SCS2S0JnzngWEiYkHNk')
  .listCollections())[0] // retrieve the first subcollection `.id`

Note that this only works with the node.js library, if you're attempting to do your query fore the front-end, it will fail. Then you should simply put a reference of your subcollection inside your parent doc by an update when creating your subcollection in the first place:

// const HighlightDbId = creating you subcollection
db.collection('highlights').doc('2SCS2S0JnzngWEiYkHNk').update({
  subcollection: HighlightDbId
});

And simply retrieve the field subcollection when you need to fetch data from its subcollection.

about 4 years ago · Juan Pablo Isaza Denunciar

0

I fixed this problem by adding the last doc :

 db.collection('highlights').doc('2SCS2S0JnzngWEiYkHNk').collection('4C4kd2QnaQhcp9knexkW').doc('XXXXXXXX').get()
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda