Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

139
Views
How to add a document with an auto generated ID into a Sub Collection using Firebase

This code works and shows an alert of { "plantID" : "2" }. but when I try to uncomment const newDoc = await addDoc(userPlants, data);, it doesn't push it to Firebase.

window.onsubmit = async function addPlant(){
    // Specify which HTML data to get from
    var selectPlants = parseInt(document.getElementById("plantID").value);

    // Get reference to the collection that we are adding Data to
    const userPlants = collection(firestore, "Users/" + "s6wnGQY3pH3oBGEyNJmZ/" + "Plants");

    const data = {
        plantID: selectPlants
    };

    alert(JSON.stringify(data));

    //const newDoc = await addDoc(userPlants, data);
}

However, if I changed the code to

async function addPlant(){
    // Specify which HTML div to use
    var selectPlants = parseInt(document.getElementById("plantID").value);

    // Get reference to the collection that we are adding Data to
    const userPlants = collection(firestore, "Users/" + "s6wnGQY3pH3oBGEyNJmZ/" + "Plants");

    const data = {
        plantID: 3
    };

    const newDoc = await addDoc(userPlants, data);
}

and add it to the window.onload function, it works and stores it in Firebase. I've checked that const data gives the proper value. I have also tried onclick, as well as adding the functions and function call directly in the HTML folder as well, all with no luck.

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!