• Jobs
  • About Us
  • professionals
    • Home
    • Jobs
    • Courses and challenges
  • business
    • Home
    • Post vacancy
    • Our process
    • Pricing
    • Assessments
    • Payroll
    • Blog
    • Sales
    • Salary Calculator

0

144
Views
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.

almost 3 years ago · Juan Pablo Isaza
2 answers
Answer question

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.

almost 3 years ago · Juan Pablo Isaza Report

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.

almost 3 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Our process Sales
Legal
Terms and conditions Privacy policy
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recommend me some offers
I have an error