4

I am trying to store a userid (type Number) in AsyncStorage but this throws an error below:

Exception '-[_NSCFNumber length]: unrecognized selector sent to instance 0xb0000000000000023 was thrown while invoking multiset on target

AsyncLocalStorage with params (((userid, 2))

Please help me resolve this.

class SignIn extends Component {

  loginHandler = async () => {
    this.setState({ loading: true });
    try {
      let { data } = await axios
        .post("http://offer.kdamjibhai.com/api/login", {
          username: this.state.username,
          password: this.state.password
        })
        .then(response => {
          if (response.data.status_code === 200) {
            if (response.data.data.status === "success") {
              //alert('came here ')
              AsyncStorage.setItem("loggedIn", "true");
              AsyncStorage.setItem('userid', response.data.data.user_info.user_id);
              
              this.setState({ loading: false });
              this.props.navigation.navigate("SignedIn");
            } 
          } else {
            alert(response.data.data.message);
            this.setState({ loading: false });
          }
        });
    } catch (err) {
      console.log(err);
    }
    
  };

  render() {}

}

export default SignIn;

2
  • do you have an account of your api for testing? Commented Jun 12, 2018 at 3:10
  • ys url : offer.kdamjibhai.com/api/login username : vipul password : 123456 you will get response Commented Jun 12, 2018 at 6:04

2 Answers 2

4

I have tried your code, the useid returned is Number, and AsyncStorage can only store strings. So you need to convert userid to string and then you can save. You should use .then().catch() to handle error instead of try{} catch{} and remove async, await keywords since you are using .then().catch() syntax.

loginHandler = () => {
  this.setState({ loading: true });
  axios
  .post("http://offer.kdamjibhai.com/api/login", {
    username: this.state.username,
    password: this.state.password
  })
  .then(response => {
    if (response.data.status_code === 200) {
      if (response.data.data.status === "success") {
        //alert('came here ')
        AsyncStorage.setItem("loggedIn", "true");
        AsyncStorage.setItem('userid', response.data.data.user_info.user_id.toString());
        this.setState({ loading: false });
        this.props.navigation.navigate("SignedIn");
      }
    } else {
      alert(response.data.data.message);
      this.setState({ loading: false });
    }
  })
  .catch((error) => {
    console.log(error);
  })
};
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, but I am getting value Promise { "_40": 0, "_55": "2", "_65": 1, "_72": null, } while I use console log AsyncStorage.getItem('userid');
2

It seems you are trying to store object or number in AsyncStorage. AsyncStorage support only string to save. If user_id is number, convert it to string or use JSON.stringify before save to AsyncStorage.

You can first transform your response to a string with JSON.stringify and when you retrieve the object again you can use JSON.parse to get the object again.

3 Comments

I tried with JSON.stringify but getting this response Promise { "_40": 0, "_55": null, "_65": 0, "_72": null, } While I use console.log(AsyncStorage.getItem('userid'))
AsyncStorage.getItem() is asynchronous, so you need to await for it before logging: const userid = await AsyncStorage.getItem('userid'); console.log(userid); Now you are logging a Promise object to the console
@Kedar Dave, your comment seems different from your question. In question you are asking about setItem and in comment you are telling about getItem. But still for your comment, getItem return a promise, you should handle promise and do on that promise result

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.