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

195
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
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