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

173
Visualizações
Why i can't pass variables with my function?

I have this code in my react native app that is using firebase :

let bestTutors = [];

const getBestTutors = async () => {
    const snapshot = await getDoc(docRef);
    bestTutors = snapshot.data().list;
}

getBestTutors();

console.log(bestTutors);

But when i run it it only console logs "Array[ ]", I've tried to using .then( ) and all kind other stuff that doesn't make sense for me, idk if there's a wey to use it without the await or How can i pass the data to the variable :( ?

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

0

call function with await. Like this one

await getBestTutors()

about 4 years ago · Juan Pablo Isaza Relatório

0

Look at my comments

Your Code 1

let bestTutors = [];

const getBestTutors = async () => {
    const snapshot = await getDoc(docRef);
    bestTutors = snapshot.data().list;
}

getBestTutors(); // Asynchronous function

console.log(bestTutors); // this is called before getBestTutors() returns the data. So console.log(bestTutors) return de default value: []

The new code

let bestTutors = [];

const getBestTutors = async () => {
    const snapshot = await getDoc(docRef);
    bestTutors = snapshot.data().list;
}

await getBestTutors(); // Wait until getBestTutors() has finished and then Next.

console.log(bestTutors);

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