Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React Native - Exception '-[NSNull length]: unrecognized selector sent to instance 0x1e6c059b0' was thrown while invoking multiSet on

I get this error few seconds after the app is loaded (but not always sometimes after few minutes or few downloads). I need to solve this issue.
if more details are needed and/or editing - please tell me and I'll make them.

Exception '-[NSNull length]: unrecognized selector sent to instance 0x1e6c059b0' was thrown while invoking multiSet on target AsyncLocalStorage with params ( ( ( "@Ye-Music:songs", "" ) ), 483 )

Screenshot of the error


The function with 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"
},
like image 496
ExtraSun Avatar asked Nov 30 '18 06:11

ExtraSun


2 Answers

When calling AsyncStorage, the value needs to be defined. If it's not, you'll get the error you encountered.

Error handling logic (for the undefined case) is probably the best way to go.

if(songsResult && songsResult.d) {
   AsyncStorage.setItem("@Ye-Music:songs", songsResult.d);
} else {
   // handle error case here
}
like image 92
calvinf Avatar answered Nov 03 '22 09:11

calvinf


Make sure songsResult.d is not undefined and is a string with a length > 0 !

like image 36
Hugo Laplace Avatar answered Nov 03 '22 09:11

Hugo Laplace