admin管理员组

文章数量:1405392

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("", {
          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;

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./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;

Share Improve this question edited Jun 1, 2019 at 8:29 Godfrey 1,11115 silver badges17 bronze badges asked Jun 12, 2018 at 2:54 Kedar DaveKedar Dave 48112 silver badges25 bronze badges 2
  • do you have an account of your api for testing? – MudOnTire Commented Jun 12, 2018 at 3:10
  • ys url : offer.kdamjibhai./api/login username : vipul password : 123456 you will get response – Kedar Dave Commented Jun 12, 2018 at 6:04
Add a ment  | 

2 Answers 2

Reset to default 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./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);
  })
};

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.

本文标签: javascriptStoring Value from JSON response to AsyncStorage React NativeStack Overflow