jasonmerino / react-native-simple-store

A minimalistic wrapper around React Native's AsyncStorage.

Home Page:https://www.npmjs.com/package/react-native-simple-store

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

get fails when there is no value saved for such key

anttitakalahti opened this issue · comments

this: return JSON.parse(value);

results in this error:
SyntaxError: Unexpected token u in JSON at position 0
at Object.parse (native)
at /Users/xxx/xxx/xxx/node_modules/react-native-simple-store/dist/index.js:24:17

when called with a key that has no value in AsyncStorage because the value is undefined and can't be parsed into JSON.

What version of React Native are you using?

Closing this due to inactivity. If this is still an issue for you please comment again with a small reproduction repo and I'll take a look at the code and see what I can do to help.

I also get this error
store.get(key) return the JSON.parse(value); the error is in here

reproduction:
1.Install android release app, native version is 1.0.0, this version use the AsyncStorage to save and get data,
2. Install react-native-simple-store replace the AsyncStorage ,then do something ensure to use Store.save(key, value);modified the native version to 1.0.1, Install the new version app(ensure that do not uninstall the app before install new version),then open app will receive the error log

i think the reason is just like @anttitakalahti said, And my react-native version as below:

    "react": "~15.4.0",
    "react-dom": "^15.4.2",
    "react-native": "0.41.2",

because when use asyncStorage, we called asyncStorage.setItem(key,value);
when use react-native-simple-store,we called store.save(key,value),but the store.save() called the
asyncStorage.setItem(key, JSON.stringify(value));
so when we use store.get(key) will get the error!

@cxy200927099 this makes sense that you would get a crash when changing over from AsyncStorage to react native simple store because of that reason you stated (the simple store JSON.stringifies everything where you previously were just passing a string value to setItem).

Unfortunately, I'm not sure of a good way around this and because of the simple store's simplicity it doesn't have any functionality to support migrations.

If you have any ideas around this at the library level I would love to hear thoughts, otherwise the best course of action is probably to run the migration in your apps code for a few versions if you still wanted to switch over to the simple store.

@jasonmerino thank reply,yes i have update my app version for using simple store.