问题
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