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

140
Vistas
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
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