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

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

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