gsklee / ngStorage

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

Parse error when an unquoted string is stored in localStorage #204

Open hackel opened 8 years ago

hackel commented 8 years ago

I'm using ngStorage with Satellizer, and because Satellizer stores a token in localStorage as a plain, unquoted string, I get a parsing error when ngStorage attempts to sync this value. This started happening when I changed the prefix ngStorage is using to match Satellizer. I will change it back and use different prefixes for now, but this work-around is sub-optimal.

It would be great if it was either possible to store plain strings, or just to exclude certain keys from syncing. I realise that I can write my own serializer to accomplish this, and that's another approach I may investigate, but it seems like this is a common enough issue that the functionality should be built-in.

egilkh commented 8 years ago

Could you provide a minimal example that fails?

nmehta6 commented 8 years ago

I am an issue with strings stored in sessionStorage as well. Here are the steps to repro:

  1. Store something in sessionStorage as a string. For example: key: 'test', value: 'testing'.
  2. Load your angular app. At this point, ngStorage is trying to run $sync which tries to call JSON.parse on the stored value. This will fail because the stored string is not quoted like mentioned by @hackel

Is there a way to turn off the auto syncying behavior, or any other workarounds anyone can suggest?

egilkh commented 8 years ago

Proper solution would be to put try/catch around serialization/deserialization. Will be done.

snebjorn commented 5 years ago

This isn't fixed yet.