Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JSON value '<null>' of type NSNull cannot be converted to NSString #9743

Closed
ThaJay opened this issue Sep 5, 2016 · 2 comments
Closed

JSON value '<null>' of type NSNull cannot be converted to NSString #9743

ThaJay opened this issue Sep 5, 2016 · 2 comments
Labels
Resolution: Locked This issue was locked by the bot.

Comments

@ThaJay
Copy link

ThaJay commented Sep 5, 2016

screen shot 2016-09-05 at 15 32 48

On Android it's not a problem, only on IOS. When you set a key `'string'` to value `undefined` it will succeed. with the following code:
function set(key, value) {
  return AsyncStorage.setItem(key, JSON.stringify(value))
}

But, if you then try to retrieve that item with the following code, the above error pops up.

function get(key) {
  function parseJson (item) {
    try { return JSON.parse(item) }
    catch (e) { return item }
  }
  return AsyncStorage.getItem(key).then(item => parseJson(item))
}

Because of the nature of the stacktrace (It does not show my code, there are too many internal function calls to have mine show up) it took me a really long time to figure out what was going on here, it was not obvious to me that AsyncStorage was producing this error.

Anyway, the following code solves it (just check instead of blindly set):

function set(key, value) {
  value = JSON.stringify(value)
  if (value) return AsyncStorage.setItem(key, value)
  else console.log('not set, stringify failed:', key, value)
}

Thoughts:

  • check for unparsable values on setItem
  • improve error message to better reflect what the problem is so we can include the check (like in my example)
@javache
Copy link
Member

javache commented Sep 9, 2016

If you're using flow, it should highlighted to you that AsyncStorage.setItem expects a non-null key and value. The error on the iOS side is expected as you can't have null entries in an array.

@muhtar-sgk
Copy link

you saved my live

@facebook facebook locked as resolved and limited conversation to collaborators May 24, 2018
@react-native-bot react-native-bot added the Resolution: Locked This issue was locked by the bot. label Jul 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Resolution: Locked This issue was locked by the bot.
Projects
None yet
Development

No branches or pull requests

5 participants