2

I am getting undefined returned value when I press button.

<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
    })
  }
}

Status value is undefined in above function which should be true or false from below function.

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;
    });
}
1
  • 1
    axios(config) -> return axios(config) or just await the result and return from the function itself. You've marked the function as async but don't use await anywhere in it. Commented Jan 25, 2021 at 13:36

2 Answers 2

2

You are not returning anything from LockUnlockAccount

You need to do

return axios(config).then(...)
Sign up to request clarification or add additional context in comments.

Comments

2

You have to returned the response from axios, which is what will be returned by the wrapping function. just add return in front of axios call.
below is your updated code

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 
    })
  };
  return 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;
    });
}

Comments

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.