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

156
Vistas
React and Next map returning undefined

So I have been working with NextAuth for a few hours and have been using the Twitter API to login/search for tweets

I have got most of the things set up with ease however there's some issues with my client side fetching oof the API routes I created on the server.

Index.js

export default function Home() {
const { data: session } = useSession()
  const [statuses, setStatuses] = useState();


 useEffect(() => {
    
 fetch('api/twitter/search')
 .then(response => response.json(),)
 .then(response => setStatuses(response))
 .catch(err => console.error(err));

 console.log(statuses)

  
 }, [])



 
 



  if (session) {
    return (
      <>
      Welcome {session.user.name}<br/>
        Signed in as {session.user.email} <br />
 
      
        {statuses.map((st) => (
         <p>{st.data.id}</p>
))}


        <button onClick={() => signOut()}>Sign out</button>
      </>
    )
  }
  return (
    <>
      Not signed in <br />
      <button onClick={() => signIn()}>Sign in</button>
    </>
  )
  

  
}

export async function getServerSideProps(context) {
  return {
    props: {
      session: await getSession(context),
    },
  }
}

I keep getting the error cannot read properties of undefined (reading 'map')

Even tho I have checked in my React dev tools that the statuses state indeed has the data I'm trying to map over.

EDIT - console.log of statuses

console.log(statuses)

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

export default function Home() {
const { data: session } = useSession()
  const [statuses, setStatuses] = useState([]);


 useEffect(() => {
    
 fetch('api/twitter/search')
 .then(response => response.json(),)
 .then(response => setStatuses(response?.data))
 .catch(err => console.error(err));

 console.log(statuses)

  
 }, [])


    return (
    <>
      {session && 
        <>
          Welcome {session?.user?.name}<br/>
          Signed in as {session?.user?.email} <br />
          {statuses?.map((st) => (<p>{st?.id}</p>))}
          <button onClick={() => signOut()}>Sign out</button>
        </>
       }
      {!session &&
        <>
          Not signed in <br />
          <button onClick={() => signIn()}>Sign in</button>
        </>
    </>
    )
}

export async function getServerSideProps(context) {
  return {
    props: {
      session: await getSession(context),
    },
  }
}

You are getting session value before your state is being set inside the useEffect. Adding ? before map would remove the error statuses?.map

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