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

305
Views
Nested Javascript async/await not being fired - Nuxt & Firebase Auth

I'm writing a basic middleware for NuxtJS 3 for authentication using Firebase and just need some help with proper chaining and nesting syntax. Here's my code:

import { onAuthStateChanged } from 'firebase/auth';
import { getDoc, doc } from 'firebase/firestore';
import { auth, db } from '~/plugins/firebase';

export default async function ({ redirect }) {
  await onAuthStateChanged(auth, (user) => {
    if (user) {
      const documentRef = doc(db, 'users', user.uid);

      const getDocData = async () => {
        const docSnap = await getDoc(documentRef);

        if (docSnap.exists()) {
          console.log('Document data:', doc.data());
        }
      };
    }
  });
}

Most of it works, but the const getDocData... isn't firing and of course it's because I'm not even calling it but I have tried for hours to come up with a clean solution and nothing seems to be working so far.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You need to declare the callback async, as follows:

onAuthStateChanged(auth, async (user) => {
  if (user) {
    const documentRef = doc(db, 'users', user.uid);
    const docSnap = await getDoc(documentRef);

    if (docSnap.exists()) {
      console.log('Document data:', docSnap.data());
    }
  }
});

Also note that you need to change doc.data() to docSnap.data().

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