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

166
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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