I am struggling to find a way to provide a callback function to correctly use the iTunes Store search API.
I am trying to achieve this behaviour:
const getiTunes = fetch(`https://itunes.apple.com/search?term=${search}&media=movie&limit=200`)
.then(results => results.json())
.then(results => {
if (results.errorMessage) throw Error(results.errorMessage)
else setResults(results)
})
.catch(error => {
//handle error
})
and so far I have this:
const getiTunes = results => {
if (results.errorMessage) throw Error(results.errorMessage)
else setITunes(results)
}
const script = document.createElement("script")
script.src = `https://itunes.apple.com/search?term=${search}&media=movie&limit=200&callback=getiTunes`
script.async = true
document.body.appendChild(script)
I keep getting the following error:
Uncaught ReferenceError: getiTunes is not defined
I have also tried &callback="getiTunes" and &callback=${getiTunes} and they also fail.
These functions are being called in an useEffect hook in React. Is there a specific way I have to retrieve the function name?
Aside
And if I try to not provide a callback function, it will work only if the search is a new search (ie I haven't searched that term before). However, if I have (say on production URL or locally) then it will error with the following:
Access to fetch at 'https://itunes.apple.com/search?term=spiderman&media=movie&limit=200' from origin 'http://localhost:3000' has been blocked by CORS policy: The 'Access-Control-Allow-Origin' header has a value "--it has the last URL I used to search for this successfully--"...
I was able to get this working by adding my script to handle the response to the document also.
Updated Answer
I have found a better solution via this code sandbox. It perfectly solves this problem by creating a function which then hands off the response to a callback function.
I then use it in the following way: