Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

229
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!