【发布时间】:2019-05-23 10:29:57
【问题描述】:
AsyncStorage 错误:未定义访问令牌
如何定义 accesstoken 我使用 php 作为后端 api 并使用 accesstoken 进行注册以存储在 Asyncstorage 中
async storeToken(accesstoken) {
try {
await AsyncStorage.setItem(AccessToken, accesstoken);
} catch (error) {
console.log('AsyncStorage error: ' + error.message);
}
}
userRegister = () =>{
const {username} = this.state;
const {password} = this.state;
if(username=="" || password==""){
alert("Please fill out all fields");
}
// else if (password!=cpassword) {
// alert("Passwords do not match.");
// }
else{
fetch('http://192.168.01.1/test/register.php', {
method: 'post',
header:{
'Accept': 'application/json',
'Content-type': 'application/json'
},
body:JSON.stringify({
username: username,
password: password,
})
})
.then((response) => response.json())
.then((responseData) =>{
// how to define accesstoken
//this.storeToken('accessToken',JSON.stringify(results));
//if using in this then same error
this.storeToken('accesstoken',responseData.accesstoken)
if(responseData == 'User Registered Successfully'){
Alert.alert(
'Success',
'User Registered Successfully',
[
// {text:'ok', onPress: () => this.props.navigation.goBack() }
]
);
//this.props.navigation.goBack()
}else{
alert(responseJson);
}
})
.catch((error)=>{
console.log(error);
});
}
}
如何调用 accesstoken 在函数中定义。如何找出这个 如果使用 await 函数来访问令牌,则会显示错误 await is not an function
【问题讨论】:
-
您将第一个变量作为硬代码字符串而不是访问令牌值传递。从此 this.storeToken(responseData.accesstoken) 替换您的 this.storeToken 函数
标签: react-native async-await asyncstorage