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.
CodePudding user response:
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.
CodePudding user response:
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.