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

229
Visualizações
Get data from google firebase's firestore, put into global object/array javascript, react

I am trying to read from firestore and push that data into a global variable. I am able to push however the array acts differently outside the function (I've attached a screenshot from the console showing the dif). For example I can call data[0] or data[1] inside the function just fine but outside it returns "undefined". Can anyone help with this? This is for a react project, so if there is a way to do it using react hooks or something like that, I can implement no problem.

let fbData = [];

const GetData = async () => {
    const q = query(collection(db, "surveys"), where("surveyComplete", "==", true));
    const querySnapshot = await getDocs(q);
    querySnapshot.forEach((doc) => {
        fbData.push({
            [doc.data().surveyId]: {
                "name": doc.data().surveyName,
                "id": doc.data().surveyId
            }            
        });
    }, 
    function (error) {
        console.log("Error getting documents: ", error);
    });
}

And yes I know the way I structure my data is weird, I'm planning on fixing it once I figure this issue out.

image of console log outside function and inside function

about 4 years ago · Juan Pablo Isaza
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