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

144
Visualizações
if function statement for each document in a firestore collection node.js

Trying to create an if statement for the 'one' and 'two' functions, however when i am creating it i can't seem to make the statement run for each document. So i took it out and ran them buy them selves and they both work fine right now, but not sure how to wrap it in a condition for it to loop through each document in a collection .

any tips or pointers will be massively appreciated

thanks

const {
    initializeApp,
    applicationDefault,
    cert,
} = require("firebase-admin/app");
const {
    getFirestore,
    Timestamp,
    FieldValue,
} = require("firebase-admin/firestore");

const firebaseConfig = {
    xxxx,
};

const serviceAccount = require("xxxxxx.json");

initializeApp({
    credential: cert(serviceAccount),
});

const db = getFirestore();

async function one() {
    const foodRef = db.collection("xxxxx");

    const snapshot = await foodRef.where("item", "==", 2).get();

    if (snapshot.empty) {
        console.log("No matching documents.");
    }

    snapshot.forEach((doc) => {
        console.log(doc.id, "=>", doc.data());

        const res = doc.ref.set(
            {
                fatIntake: "50",
            },
            { merge: true }
        );
    });
}

async function two() {
    const foodRef = db.collection("xxxxxx");

    const snapshot = await foodRef.where("item", "==", 1).get();

    if (snapshot.empty) {
        console.log("No matching documents.");
    }

    snapshot.forEach((doc) => {
        console.log(doc.id, "=>", doc.data());

        const res = doc.ref.set(
            {
                fatIntake: "10",
            },
            { merge: true }
        );
    });
}

one();
two();
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

It seems like you have wrong if condition

In function one you put the following if statement

const snapshot = await foodRef.where("item", "==", 2).get();

You put item == 2 instead of item == 1. No mistake in the condition but the logic I suppose. Atleast what I think is the mistake. And the same problem is with function two

Also, confirm the field name item. This should exactly match the name in firestore, and it shouldn't be inside any other sub map.

If that is not the case and not what you asked then please elaborate more and precisely what your problem is.

The syntax and call to the if statement is correct. Actualy it is a query not an if statement. so, your query is syntatically correct.

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