0

Why I am getting this error though there is no syntax error?What is the reason behind this error?

VM1503:1 Uncaught SyntaxError: Unexpected end of JSON input at JSON.parse ()


<script>
    if (localStorage.getItem('cart') == null) {
        console.log("NULL");
        var cart = [];
    } else {
        cart = JSON.parse(localStorage.getItem('cart'));
    }



    $('.cart').click(function() {
        console.log("Clicked");
        var idstr = this.id.toString()
        if (cart[idstr] != undefined) {
            cart[idstr] = cart[idstr] + 1
        } else {
            cart[idstr] = 1
        }
        localStorage.setItem('cart', JSON.stringify(cart))
        console.log(cart);
    });
</script>


  • Impossible to tell without more information about `cart` – KooiInc Apr 28 '20 at 04:16
  • One case when this could occur is if the key 'cart' exists but it has no value. Instead of checking for null. You could try `if (localStorage.getItem('cart')) { ... } ` – Siddharth Seth Apr 28 '20 at 04:20
  • https://stackoverflow.com/questions/51118396/uncaught-syntaxerror-unexpected-end-of-json-input-at-json-parse-anonymous – Aman Apr 28 '20 at 04:32

0 Answers0