反应本地AsyncStorage Firebase JSON值'< null>'类型的NSNull不能转换为NSString [英] react native AsyncStorage firebase JSON value '<null>' of type NSNull cannot be converted to NSString
问题描述
我正在使用AsyncStorage
存储一些Firebase用户数据.
I am using AsyncStorage
to store some Firebase user data.
AsyncStorage.setItem('firebaseUser', JSON.stringify(data))
当我尝试检索数据时,React Native会出现以下错误
React Native is blowing up with the below error when I try to retrieve the data
AsyncStorage.getItem('firebaseUser')
无法将类型为NSNull的JSON值''转换为NSString + [RCTConvert NSString:] + 226 RCTConvert.m:56
JSON value '' of type NSNull cannot be converted to NSString +[RCTConvert NSString:] + 226 RCTConvert.m:56
Firebase似乎返回了一些null
数据(displayName
,photoUrl
等).不确定是否相关.
It appears that Firebase comes back with some null
data (displayName
, photoUrl
, etc.) Not sure if it's related.
版本:
"react-native": "^0.31.0",
"firebase": "^3.3.0",
更新:
实际上,我将其范围缩小到Firebase模块正在使用AsyncStorage
存储其自己的数据.发生这种情况时,我收到JSON <null>
错误.当我清除AsyncStorage
时,它将再次起作用.这是Firebase数据对象的样子:
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
}"
推荐答案
AsyncStorage希望您在调用setItem
时给它一个string
,但是您正在给它一个对象.在保存Firebase数据之前,先对其进行字符串化处理:
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);
这篇关于反应本地AsyncStorage Firebase JSON值'< null>'类型的NSNull不能转换为NSString的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!