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

279
Vistas
How to list all documents in a sub-collection and then fetch their documents by their ID?

I'm trying to show data from a document inside a collection between "students" and their "courses"

I'm using html, javascript and this structure but as of now I can only retrieve the ID of the document but I'd like to also access the data inside this document

async function fetchCourses() {

  const studentId = firebase.auth().currentUser.uid
  const courseIds = await db.collection(`students/${studentId}/attending`).get();

  const courseDocs = await Promise.all(
    courseIds.docs.map(doc => db.doc(`courses/${doc.id},${studentId}`).get())
  );

  return courseDocs.filter(doc => doc.exists).map(doc => ({ id: doc.id, ...doc.data() }),
  console.log(courseDocs));
}

My console.log displays all this:

Console.log

Any help is gladly appreciate

Edit

This is the data structure inside my Firebase:

The students are stored in a collection indexed by their uid and each student document contains their name and more data, but most importantly a sub-collection named attending which contains empty documents indexed by courseId, refering to a document in the courses collection

enter image description here

The courses are stored in another collection indexed by courseId and like in students each course document contains a title and more data, and a sub-collection named attendees which contains empty documents indexed by uid, refering to a student.

enter image description here

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

import { doc, getDoc } from "firebase/firestore";

const docRef = doc(db, "cities", "SF");
const docSnap = await getDoc(docRef);

if (docSnap.exists()) {
  console.log("Document data:", docSnap.data());
} else {
  // doc.data() will be undefined in this case
  console.log("No such document!");
}

above is an example from firestore docs. firestore has really specific syntax. you can only access data of request with doc.data() syntax. All other attempts will bring you nothing or some irrelevant object(at least in my experience)

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