Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

196
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda