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

314
Visualizações
Unhandled Runtime Error TypeError: subSector.map is not a function

I'm unable to map through json data from a fake api. It's working when returning more than one data but this error happens when the data is a single object.

Here is my code:

    {subSector
     ? subSector.map((sector) => (
//               ^ ~ subSector.map is not a function
         <MenuItem value={sector.id}>{sector.email}</MenuItem>
       ))
    : "nodata"}
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

By using map on subSector, you are assuming it is an array of items and not a single item.

You are seeing the error because subSector is not an array, and therefore does not have the map function available.

Instead, either access the subSector properties directly (assuming subSector is a single item):

    {subSector ? (
         <MenuItem value={subSector.id}>{subSector.email}</MenuItem>
       )
    : "nodata"}

or handle both cases where it could be an array or single item:

    {Array.isArray(subSector)
     ? subSector.map((sector) => (
         <MenuItem value={sector.id}>{sector.email}</MenuItem>
       ))
    : subSector ? (
         <MenuItem value={subSector.id}>{subSector.email}</MenuItem>
       ) 
    : "nodata"}

or, as catgirlkelly says, ensure your fake data always returns an array, even for a single item.

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