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

170
Visualizações
items.map is not a function javascript

So Im trying to get insta posts by url

This is my code:

const data = get('${url}'})

console.log(data)

Data that i get in console (post with only 1 picture):

[
{
media: "example.com"
}
]

const items = data.media

so the issue is right here:

const Post_MP4 = items.map((item, index) => {

if(item && item.includes(".mp4"))
 return ({name: 'video${index}.mp4', attachment: item})

else if(item && item.includes(".jpg")) 

return ({name: 'image${index}.jpg', attachment: item})
}).filter(item => item)

When the insta post has only 1 picture i get an error(items.map is not a function) but when the post has more than 1 picture I dont get any error.

Data when the post has more than 1 picture:

{
media: ["example", "example2"] 
} 

What can I do to not get any error by using this code. Thank you.

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

The map method is to be used on an array. When there's only one picture, you return a string, which do not have the array method. But when there's more than one, you return an array which indeed has the map method. As mentioned in the comments, check if media is an array with the isArray method.

about 4 years ago · Juan Pablo Isaza Relatório

0

The "TypeError: map is not a function" occurs when we call the map() method on an object that is not an array. To solve the error, console.log the value you're calling the map() method on and make sure to only call the map method on valid arrays.

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