18

I have read online that the unexpected token u issue can come from using JSON.parse(). On my iPhone 5 there is no problem, but on my Nexus 7 I get this sequence of errors:

enter image description here View large

I realize this is a duplicate, but I am not sure how to solve this for my specific problem. Here is where I implement JSON.parse()

 $scope.fav = []; 

if ($scope.fav !== 'undefined') {
   $scope.fav = JSON.parse(localStorage["fav"]);
}
benjipelletier
  • 653
  • 5
  • 11
  • 29
  • 1
    I'd say a good first step would be `console.log(localStorage["fav"])` and see what that gives you on the Nexus – ivarni Apr 22 '14 at 14:01
  • 2
    Could you show the output of `console.log(typeof localStorage["fav"]);` and `console.log(typeof JSON.stringify($scope.fav));` right before your `JSON.parse`. It should not be `undefined` but probably there is a problem with either the `localStorage` (e.g. that the data is to large) or with the `JSON.stringify`. The `Unexpected token u` indicates that it is `undefined` – t.niese Apr 22 '14 at 14:01
  • The addToFav function only gets called on a button click. These buttons don't show up on the Nexus, but only on the iPhone. Should I see the output on there? – benjipelletier Apr 22 '14 at 14:03
  • As the `addToFav` is not called on your Nexus, then the error most likely happens at another place, are you really sure that the code you posted is the one where the error happens? Or did I misunderstand your comment? Showing the output of the iPhone would not really make sense if there is no error there. – t.niese Apr 22 '14 at 14:07
  • @t.niese Sorry that was the wrong place where the problem occurs. I have updated the question with the code that is called on device load up. When I think back now I believe the main issue is caused by JSON.parse(localStorage["fav"]); not having any value since this is the first time I loaded the app on this Nexus. Data had always been stored before this on the iPhone. I think the term 'undefined' is incorrect here since it was defined the line before. – benjipelletier Apr 22 '14 at 14:11

4 Answers4

20

Base on your updated question the if condition does not make sense, because you set $scope.fav to [] right before, so it can never be "undefined".

Most likely you want to have your test that way:

if (typeof localStorage["fav"] !== "undefined") {
  $scope.fav = JSON.parse(localStorage["fav"]);
}

As i don't know if there is a situation where localStorage["fav"] could contain the string "undefined" you probably also need test for this.

if (typeof localStorage["fav"] !== "undefined"
    && localStorage["fav"] !== "undefined") {
  $scope.fav = JSON.parse(localStorage["fav"]);
}
t.niese
  • 39,256
  • 9
  • 74
  • 101
  • Hi, thank you. that was the solution I was looking for. I only do this test to make sure that if fav had previous values in it then they stay there on every load up. – benjipelletier Apr 22 '14 at 14:22
  • I just came across a similar bug with my code. Checking for both typeof "undefined" and string "undefined" was my solution. Determining how "undefined" got set is another bug :) Thanks t.niese! – jjwdesign Jun 20 '16 at 19:26
12

One way to avoid the error (not really fixing it, but at least won't break):

$scope.fav = JSON.parse(localStorage["fav"] || '[]');

You're getting that error because localStorage["fav"] is undefined.

Try this and you'll understand all by yourself:

var a = undefined;
JSON.parse(a);
avetisk
  • 11,651
  • 4
  • 24
  • 37
  • `if ($scope.fav !== 'undefined') ` is useless, as `$scope.fav` is never `"undefined"` for the given code. So either remove it and only use the `localStorage["fav"] || '{}'` or move the test to the `if` condition. – t.niese Apr 22 '14 at 14:21
  • Well, I'm justing fixing one line that breaks, not his whole code :) – avetisk Apr 22 '14 at 14:22
  • But you're right, I should provide a clean code as an example. Change done :) – avetisk Apr 22 '14 at 14:28
5

Unexpected token: u almost always stems from trying to parse a value that is undefined.

You can guard against that like this:

if (localStorage['fav']) {
  $scope.fav = JSON.parse(localStorage['fav'];
}
Dave Ward
  • 59,815
  • 13
  • 117
  • 134
0

In my case, the problem was I was getting the value as localStorage.getItem[key] whereas it should have been localStorage.getItem(key).

The rest and normally faced issues have been better explained already by the above answers.

Black Mamba
  • 13,632
  • 6
  • 82
  • 105