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

153
Visualizações
getDocs - firebase react native

I want to get a document and to update. I tried used this code, but he dont accept the "idDoc":

const Doc = query(collection(database, "user_veic"),where("email", "==", auth.currentUser?.email),where("kmF", "==", ""));
        getDocs(Doc).then((querySnapshot) => {
            querySnapshot.forEach((doc) => {
                console.log(`${doc.id} => ${doc.data()}`);
                const idDoc = doc.id
            })
        })
        .then(
            updateDoc(doc(database, "user_veic", idDoc), {
            kmF: "teste1",
            km: "teste1",
            }))

^^^^: FirebaseError: Invalid document reference. Document references must have an even number of segments, but user_veic has 1

I tried this:

const Doc = query(collection(database, "user_veic"),where("email", "==", auth.currentUser?.email),where("kmF", "==", ""));
        getDocs(Doc).then((querySnapshot) => {
            querySnapshot.forEach((doc) => {
                console.log(`${doc.id} => ${doc.data()}`);
                const idDoc = doc(database, "user_veic", doc.id)

                updateDoc(idDoc, {
                    kmF: "teste1",
                    km: "teste1",
                })
            })
        })

^^^^: [Unhandled promise rejection: TypeError: doc is not a function. (In 'doc(database, "user_veic", doc.id)', 'doc' is an instance of lh)]

What did i do wrong?

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

In your first code example, you declare const idDoc inside of the callback parameter to .forEach(). That variable does not exist outside of the callback function. You then try to use it in the updateDoc() in a completely different block of code. It is undefined at that point, thus you are getting an error that you aren't passing enough parameters.

In your second code example, which is much closer to what you want to do, based on the error message it looks like you aren't importing doc with the rest of the Firestore functions from firebase/firestore.

about 4 years ago · Juan Pablo Isaza Relatório

0

RESOLVIDO @Greg thank you

const Doc = query(collection(database, "user_veic"),where("email", "==", auth.currentUser?.email),where("kmF", "==", ""));

getDocs(Doc).then((querySnapshot) => {
    let values = null;
    querySnapshot.forEach((doc) => {
        console.log(`${doc.id} => ${doc.data()}`);
        values = doc.id;
    });

    var transactionUpdate = database.collection("user_veic").doc(values);
    transactionUpdate.update({
        kmF: kmF,
    })    
})
about 4 years ago · Juan Pablo Isaza Relatório
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