【发布时间】:2021-01-25 21:37:37
【问题描述】:
当我按下按钮时,我得到未定义的返回值。
<Button btnType="outline" onClick={LockUnlockAccountHandler}>
lock client’s account
</Button>
const LockUnlockAccountHandler = async () => {
const status = await LockUnlockAccount(detailData?.userId, detailData?.blocked ? 'UNLOCK' : 'LOCK');
console.log(status)
if(status){
setDetailData({
...detailData,
blocked: !detailData.blocked
})
}
}
状态值在上面的函数中是未定义的,从下面的函数中应该是真或假。
export async function LockUnlockAccount(clientID, dataVal) {
var config = {
method: 'post',
url: endpoint.lockAccount + clientID + "/status" + endpoint.key,
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
data : qs.stringify({
'status': dataVal
})
};
axios(config)
.then(function (response) {
//console.log(JSON.stringify(response));
if (response.status === 200) {
toast('succès');
return true;
}
return false;
})
.catch(function (error) {
console.log(error);
toast('error');
return false;
});
}
【问题讨论】:
-
axios(config)->return axios(config)或只是await结果并从函数本身返回。您已将该函数标记为异步,但不要在其中的任何位置使用await。
标签: javascript reactjs asynchronous async-await