firebase - 无法将 NSNull 类型的 native AsyncStorage firebase JSON 值 '<null>' 转换为 NSString

标签 firebase react-native firebase-authentication

我正在使用 AsyncStorage存储一些 Firebase 用户数据。

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

当我尝试检索数据时,React Native 出现以下错误
AsyncStorage.getItem('firebaseUser')

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



Firebase 似乎返回了一些 null数据( displayNamephotoUrl 等)不确定是否相关。

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

更新:
我实际上将其缩小到 Firebase 模块正在使用 AsyncStorage存储自己的数据。发生这种情况时,我会得到 JSON <null>错误。当我清除 AsyncStorage ,它再次工作。 Firebase 数据对象如下所示:
"{
  "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 希望您给它一个 string调用setItem时,但你给它一个对象。在保存之前尝试对 firebase 数据进行字符串化:

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

当你读回它时解析它:
AsyncStorage.getItem('firebaseUser').then(data => data ? JSON.parse(data) : null);

关于firebase - 无法将 NSNull 类型的 native AsyncStorage firebase JSON 值 '<null>' 转换为 NSString,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39059800/

相关文章:

Firebase 云消息传递不适用于三星互联网

firebase - 如何只检索新数据?

android - 复制到 APK lib/x86/librealm-jni.so 中的重复文件

firebase - Flutter 如何允许用户创建和切换多个账户?

javascript - Firebase OnAuthStateChanged 函数不返回任何内容

javascript - React Native/Firebase Messaging - 永远不会发送消息/通知

ios - React-native iOS,异步存储错误: "Invalid key - must be at least one character. Key: "

reactjs - 我可以将 NativeBase 与 Mobx 一起使用吗?

javascript - 如何将我的 expo 应用程序发布到 Apple 应用商店?

ios - Firebase 无密码电子邮件身份验证不会在 iOS 上打开应用程序