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

201
Visualizações
Pulling a variable out of Promise when querying data from Firestore Firebase database - Javascript
let savedArrayUID = []; let savedArrayEmails = [];

function pullIt(emailSearch) {

    db.collection(collectionName).where('email', '==', emailSearch).get()
        .then((querySnapshot) => {
            querySnapshot.forEach((doc) => {
                savedArrayUID.push(doc.id);
                savedArrayEmails.push(doc.data());
                // doc.data() is never undefined for query doc snapshots

                console.log(doc.id, " => ", doc.data());
                // saved.   push(doc.id);
                return savedArrayUID; 
            })
        });
}

I can query the data from the database but cannot pull the variable out of the scope of the function. I want to use this function to pass through emails to find info of their profile saved in my Database.

I really struggle to understand how Promiseses can help here. I have a feeling this is already solved, but I could not find an answer anywhere.

about 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

There's two steps to this:

  1. Ensure that your data makes it out of your pullIt (as a promise).
  2. Then call that pullIt correctly, waiting for the promise to resolve.

In code, you're missing a top-level return the pullIt code:

function pullIt(emailSearch) {
    // 👇
    return db.collection(collectionName).where('email', '==', emailSearch).get()
        .then((querySnapshot) => {
            querySnapshot.forEach((doc) => {
                savedArrayUID.push(doc.id);
                savedArrayEmails.push(doc.data());
            })
            return savedArrayUID; // 👈
        });
}

And then when calling pullIt, you'll need to use either await or then, to ensure pullIt completed before you try to access the result.

So either:

pullIt("yourSearchTeam").then((results) => {
  // TODO: use your results here
})

Or (in an async context):

const results = await pullIt("yourSearchTeam")

// TODO: use your results here
about 4 years ago · Santiago Trujillo 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