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

72
Visualizações
how to handle a promise in firebase query when there is an if statement

I try to loop through all my documents and check if there is a matching between an object Id and my id then push the data into an array , my problem is that I can not get back any data my array is always empty here is my code :

   let d = []
   let b;
    const all = await db.collection('cars')
    .get()
       .then(documentSnapshot  => 
          {  if (!documentSnapshot.empty) {
              documentSnapshot.map(async (doc) => {
                         idd = await doc.data().user.id;
                         if(idd == 'myid'){
                           b = doc.data();
                           d.push(b) 
                       }
              
              });
          }
          }
          );

      return  Promise.all(all).then(() => setActiveSpinner(false), setThereIsData(true)).catch(e => console.log(e))
about 4 years ago · Santiago Gelvez
1 Respostas
Responde à pergunta

0

You have different issues within your implementation.

First, you are mixing both plain Promise objects with callback handlers along with async/await imperative usage which usually leads to un-wanted program behavior.

You are miss-collecting the different matching documents using the Array.prototype.map operator without neither returning a result for each element nor for the transformed result.

Here down a trimmed implementation using the async/await pattern:

const result = [];
const querySnapshots = await db.collection('cars').get();
for (const document of querySnapshots.docs) {
  const data = document.data();
  if (data.user.id === 'myid') {
    result.push(b);
  }
}
setActiveSpinner(false);
setThereIsData(true);
return result;
about 4 years ago · Santiago Gelvez 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