react native AsyncStorage firebase JSON value '<null>' of type NSNull cannot be converted to NSString

北城余情 提交于 2019-12-10 18:47:49

问题


I am using AsyncStorage to store some Firebase user data.

AsyncStorage.setItem('firebaseUser', JSON.stringify(data))

React Native is blowing up with the below error when I try to retrieve the data

AsyncStorage.getItem('firebaseUser')

JSON value '' of type NSNull cannot be converted to NSString +[RCTConvert NSString:] + 226 RCTConvert.m:56

It appears that Firebase comes back with some null data (displayName, photoUrl, etc.) Not sure if it's related.

Versions:

"react-native": "^0.31.0",
"firebase": "^3.3.0",

Update: I actually narrowed it down to the Firebase module is using AsyncStorage to store its own data. When that happens, I get the JSON <null> error. When I clear AsyncStorage, it works again. Here's what the Firebase data object looks like:

"{
  "uid": "XwsK2",
  "displayName": null,
  "photoURL": null,
  "email": "test@test.com",
  "emailVerified": false,
  "isAnonymous": false,
  "providerData": [
    {
      "uid": "test@test.com",
      "displayName": null,
      "photoURL": null,
      "email": "test@test.com",
      "providerId": "password"
    }
  ],
  "apiKey": "xLxzuyPlUqA88kEfy",
  "appName": "[DEFAULT]",
  "authDomain": "abc.firebaseapp.com",
  "stsTokenManager": {
    "apiKey": "xLxzuyPlUqA88kEfy",
    "refreshToken": "AJiUa4Y7b",
    "accessToken": "eyJhbGciOiJSUzjQzYzU3Nzk5NGVmNGNiy_DlQ8xQO7kq3YXxgntE9Q8YJA",
    "expirationTime": 1472098951672
  },
  "redirectEventId": null
}"

回答1:


AsyncStorage expects you to give it a string when calling setItem, but you are giving it an object. Try stringifying the firebase data before you save it:

AsyncStorage.setItem('firebaseUser', JSON.stringify(data));

And parsing it when you read it back:

AsyncStorage.getItem('firebaseUser').then(data => data ? JSON.parse(data) : null);


来源:https://stackoverflow.com/questions/39059800/react-native-asyncstorage-firebase-json-value-null-of-type-nsnull-cannot-be

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!