Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

how to add an object to a document in firebase

In my code, I get an object from an existing Firebase collection and then I want to add it to another document as an object, however I don’t understand how to do it

  // Create a query against the collection.
const q = query(subjectRef, where("Subject", "==", subject), where("Board", "==", examBoard), where("Level", "==", level));
onSnapshot(q, (snapshot) => {
  snapshot.docs.forEach(doc => {
    //add a document to a subcollection in the collection users
    const auth = getAuth();
    //editted so onAuthState function is not called
    const uid = auth.currentUser.uid
    const docRef = doc(db, "users", uid);
    const colRef = collection(docRef, "subjects");
    addDoc(colRef, {
      //?????
    });
  })

the doc would be added to the collection subjects, this is what the doc looks like:
collection subject > document
and the structure of the users is (collection) users > (document) user > (collection) subjects > (document) what i want to add

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You Can Get The User ID From auth.currentUser.uid Thing But Here’s How You Would Achive This

const q = query(subjectRef, where("Subject", "==", subject), where("Board", "==", examBoard), where("Level", "==", level));
onSnapshot(q, (snapshot) => {
  snapshot.docs.forEach(doc => {
    //add a document to a subcollection in the collection users
    const auth = getAuth();
    var q = doc.data()
    const colRef = collection(db, "users", auth.currentUser.uid,"subjects");
    addDoc(colRef, {
      Board:q.Board
      Level:q.Level
      Subject:q.Subject
    });
    });
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading