1

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.

1
  • 3
    Check to see if it’s an array? Commented Aug 6, 2022 at 21:43

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

3 Comments

When the has has only 1 picture this is what i got in console: { media: "url"}. Post with more than 1 picture: { media: ["url"] } . What can i do
@AlejoRios Check to see if it’s an array?
@DaveNewton , if data it's an array, else if isn't an array? I'll try, thank you;)
1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.