Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

143
Visualizações
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 à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda