I tried too many question solutions from stack overflow but still i can't solve this issue and i have no idea where i'm going wrong this thing as i'm new to JavaScript coding.
Here is the code:
function SaveFavMovie(id)
{
let movies = moviesData.movies;
let movieid = -1;
for (let i = 0; i < movies.length; i++) {
if (movies[i].id === id) {
movieid = id;
}
}
if(!movieid) return;
var a = [];
a = JSON.parse(localStorage.getItem('moviedb'));
a.push(id);
localStorage.setItem('moviedb', JSON.stringify(a));
return true;
Here is the error get from the above code
Uncaught TypeError: a.push is not a function
at Object.SaveFavMovie
This is the line it shows the error
a.push(tmp);
The webstore 'moviedb' is empty and trying to push data for first time.
a = JSON.parse(localStorage.getItem('moviedb'));
If the entry does not exist in localStorage (yet), it cannot parse anything and will therefore set a to null
. And thats not an array. So in the case that it is not stored yet, you might wanna take an array instead:
JSON.parse(localStorage.getItem('moviedb')) || [];
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.