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
2 Answers
Reset to default 4I 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
版权声明:本文标题:javascript - Storing Value from JSON response to AsyncStorage React Native - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744875122a2629869.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论