gsklee / ngStorage

localStorage and sessionStorage done right for AngularJS.
MIT License
2.33k stars 461 forks source link

SyntaxError: Unexpected token u at Object.parse (native) #192

Open VickyLiao opened 8 years ago

VickyLiao commented 8 years ago

i received error after i put the codes in WelcomeCtrl.js app.controller('WelcomeCtrl',['$scope','$localStorage',function($scope,$localStorage){ }])

SyntaxError: Unexpected token u at Object.parse (native) at fromJson (http://localhost:5000/app/libs/angular.js:1250:14) at Object.p.$sync (http://localhost:5000/app/libs/ngStorage.min.js:1:1628) at $get (http://localhost:5000/app/libs/ngStorage.min.js:1:1855) at Object.invoke (http://localhost:5000/app/libs/angular.js:4473:17) at http://localhost:5000/app/libs/angular.js:4290:37 at getService (http://localhost:5000/app/libs/angular.js:4432:39) at Object.invoke (http://localhost:5000/app/libs/angular.js:4464:13) at extend.instance (http://localhost:5000/app/libs/angular.js:9093:34) at nodeLinkFn (http://localhost:5000/app/libs/angular.js:8205:36)

i also have app.js var app=angular.module("myApp",["ngRoute","ngResource","ngStorage"]);

app.config(function($routeProvider){ $routeProvider.when("/create_project",{ templateUrl:"/app/components/project/create_project.html" }) $routeProvider.when("/login",{ templateUrl:"/app/components/user/login.html" }) $routeProvider.otherwise({ templateUrl:"/app/components/welcome/welcome.html" }) });

app.controller('AppCtrl',['$scope',function($scope){

}])

saqueib commented 8 years ago

:+1: and I am using angular "angular": "~1.4.2",, same error while trying to inject $localStorage into controller.

$sessionStorage works, I am using https://github.com/Swiip/generator-gulp-angular to build angular app

tangoabcdelta commented 8 years ago

this unexpected token often occurs during parsing failures, got to investigate further. meanwhile, I'm struggling to make angular 1.4.0 work with it

RealJTG commented 8 years ago

I guess "u" in SyntaxError: Unexpected token u at Object.parse (native) is for "undefined" You can accidentally save something weird to local storage like this:

angular.module('app', ['ngStorage'])
.controller('MyController', ['$localStorage', function($localStorage) {  
  var vm = this;
  vm = $localStorage.$default({
    foo: 1,
    bar: 2
  });
  // ... more code ...
  vm.total = function total() { return 123;}
}]);

http://plnkr.co/edit/LcN3PkDCFtMnUkJByLCj?p=preview will fail on deserializer() call on second run.

saqueib commented 8 years ago

I was able to fix it by removing prefix from config block

.config(['$localStorageProvider',
    function ($localStorageProvider) {
        //$localStorageProvider.setKeyPrefix('wf'); removing this fixed above error
    }])