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:
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
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.
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)