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

206
Vistas
UseEffect to fetch user number and pass to CurrentUser state on unsubscribe (order of execution)

I'm using Firebase to authenticate a user, then using the Firebase ID (uid) to setUserData with the user's row in Postgres via a HTTP request. It works as written, but I'm having trouble with the order execution of these functions because the console is returning 'invalid input syntax for type integer: "undefined"'.

The desired order is

  1. Wait for the authentication to return a uid
  2. Execute the HTTP request using uid
  3. Redirect to "/"

Instead, it seems to run 2, 1, 3, 2. On the second HTTP attempt data is populated. This explains why I sometimes need to log out and log back in for certain components to load with the user's data.

What am I missing to ensure correct order of operations?

Login.js

   async function handleSubmit(e) {
        e.preventDefault()
    
        try {
          setError("")
          setLoading(true)
          await login(emailRef.current.value, passwordRef.current.value)
          setLoading(false) 
          history.push("/")   
        } catch {
          setLoading(false)  
          setError("Failed to log in")
        }

Auth.js

   useEffect(() => {

        const fetchProfile = (uid) => {
            axios.get(`/user/${uid}`)
            .then(async (response) => {
                setUserData(await response.data)
                console.log(response.data)
                console.log(uid)
            })
            .catch(error => console.error(`Error: ${error}`))
        }

        const unsubscribe = auth.onAuthStateChanged(async user => {
            user && fetchProfile(user.uid)
            setCurrentUser(user)
            setLoading(false)
        })

        return unsubscribe

    }, [])

    const value = {
        currentUser,
        userData,
        login,
        signup,
        logout,
        resetPassword,
        updateEmail,
        updatePassword
    }

    return (
        <AuthContext.Provider value={value}>
            { !loading && children}
        </AuthContext.Provider>
    )
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

I would remove fetchProfile as a function and move unsubscribe outside of useEffect and have currentUser as a dependency to useEffect. useEffect would only run when currentUser has changed.

...
const unsubscribe = auth.onAuthStateChanged(async user => {
        //only if there is a user setCurrentUser
       if(user){
         setCurrentUser(user)
         setLoading(false)
       }
   })

useEffect(() => {
        if(!currentUser) return
        axios.get(`/user/${currentUser.uid}`)
          .then(async (response) => {
              setUserData(await response.data)
              console.log(response.data)
              console.log(uid)
          })
          .catch(error => console.error(`Error: ${error}`))
    }, [currentUser])
...
about 4 years ago · Juan Pablo Isaza 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