React Native-异常'-[NSNull长度]:在调用multiSet时抛出了发送给实例0x1e6c059b0的无法识别的选择器

时间:2018-11-30 06:05:17

标签: react-native exception exception-handling asyncstorage

在加载应用后几秒钟,我收到此错误(但有时几分钟或下载几下后,并非总是如此)。我要解决这个问题。
如果需要更多详细信息和/或进行编辑-请告诉我,我会做。

异常'-[NSNull长度]:在使用参数调用目标AsyncLocalStorage上的multiSet时,引发了发送给实例0x1e6c059b0的无法识别的选择器(         (                 (             “ @ Ye-Music:songs”,             ”         )     ),     483 )

Screenshot of the error


AsyncStorage的功能:

allSongs = () => {
    console.log('hello function!');
    fetch(URL + "/SongsList", {
      body: null,  //// sending null because its a view from SQL db
      method: "POST",
      headers: {
        Accept: 'application/json',
        "Content-type": "application/json; charset=UTF-8"
      }
    })
      .then(res => { return res.json()})
      .then((songsResult) => {
        AsyncStorage.setItem("@Ye-Music:songs", songsResult.d);
      })
      .catch(err => {
        console.error(err);
      });
  };


package.json

"dependencies": {
    "@expo/samples": "2.1.1",
    "expo": "29.0.0",
    "react": "16.3.1",
    "react-native": "https://github.com/expo/react-native/archive/sdk-29.0.0.tar.gz",
    "react-native-elements": "^0.19.1",
    "react-native-material-cards": "^1.0.9",
    "react-native-music-control": "^0.7.3",
    "react-native-music-player-service": "^0.1.4-beta",
    "react-native-search-header": "^0.3.0",
    "react-native-sound": "^0.10.9",
    "react-native-track-player": "^0.2.5",
    "react-navigation": "^2.9.3"
},

2 个答案:

答案 0 :(得分:0)

确保songsResult.d未被定义,并且是长度大于0的字符串!

答案 1 :(得分:0)

在调用AsyncStorage时,需要定义该值。如果不是,则会得到您遇到的错误。

错误处理逻辑(对于未定义的情况)可能是最好的解决方法。

if(songsResult && songsResult.d) {
   AsyncStorage.setItem("@Ye-Music:songs", songsResult.d);
} else {
   // handle error case here
}