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

236
Visualizações
Saving firebase firestore data to a variable in code

New to Firebase and JS and have been stuck on this for a long time (really thankful for any help)

Intention

Fetch data from a Firestore collection 'users' from a document named after the uid of the current user. From that document, I want to save the value of a 'company' field to a variable, to later filter using that value.

Issue

I get 'undefined' when calling the getUserCompany function from the Dashboard function.

Possible error cause

I think the issue might be that because the getUserCompany function is asynchronous, I cannot call it from a non-async function (or at least get the value of the Promise)

What should I do to produce the intended outcome? (might require a completely different approach)

NB: projectFirestore is my firebase.firestore()

function getUserCompany() {

  const user = projectAuth.currentUser;

  if (user !== null) {
    const userUID = user.uid // correct userUID (works to this point)

    projectFirestore.collection('users').doc(userUID).get().then((snapshot) => {
      const usercompany = snapshot.data().company
      console.log(usercompany) // Logs correctly = company of the current user
      return usercompany

    })
  }}

export default function Dashboard() {

  const value = getUserCompany()
  console.log(value) // logs 'undefined'

  /* ...Later used to filter */
}

Firestore database

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