Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

227
Vistas
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 la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda