Why I always get “Uncaught SyntaxError: Unexpected token u ” from Chrome?

后端 未结 1 526
北恋
北恋 2020-12-15 21:43

I have spent a whole day on this, googling and searching for answers but still could not figure out.

My code is a bit long and it works well in Firefox but gets \"U

相关标签:
1条回答
  • 2020-12-15 22:06

    At some point, something you did corrupted the value of your LocalStorage for that key. LocalStorage can only store strings, so if you pass anything else to it, it will convert it to a string. Since your value is 'undefined', that means that at some point, you probably did something like this on accident:

    var value;
    localStorage.setItem('key', value);
    

    In this case, value is undefined, which is not a string. When this gets saved, it will be converted however. Unfortunately, "undefined" is not valid JSON. That means that when it tries to parse, it will throw an exception.

    To fix your issue, you should clear the bad value out with removeItem.

    localStorage.removeItem("customerDatabase");
    
    0 讨论(0)
提交回复
热议问题