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

194
Views
Wait for UserID before accessing firebase documents in react

After a user signs in to my program, they get redirected to a page where their profile data is fetched, however I need to wait on the page for the current user to not be null before reading the data from firebase.

Inside user page:

  const currentUser = useAuth();
  const routineBundleRef = doc(db, "RoutineBundle", currentUser.uid);

  useEffect(() => {
    const getRoutines = async () => {
      const data = await getDocs(collection(routineBundleRef, "Routines"));
      setRoutines(data.docs.map((doc) => ({...doc.data(), id: doc.id})));
    };
    getRoutines();
  }, []);

Inside auth page:

const useAuth = () => {
  const [currentUser, setCurrentUser] = useState();

  useEffect(() => {
    const unsub = onAuthStateChanged(auth, user => { setCurrentUser(user)});
    return unsub;
  }, [])

  return currentUser;
}

The page is being accessed with a userid, but I think the problem is that it does not load in time before trying to access user documents.

How would I go about waiting for usedid, before accessing documents?

No ID error message image

Thanks for any help.

about 4 years ago · Santiago Gelvez
1 answers
Answer question

0

The user in onAuthStateChanged can be null. Try adding an if statement to check that:

useEffect(() => {
  const unsub = onAuthStateChanged(auth, user => {
      if (user) {
        setCurrentUser(user)
      });
  }
  return unsub;
}, [])
about 4 years ago · Santiago Gelvez 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!