如何在异步中间件函数react-redux中处理异常

时间:2019-04-02 20:13:11

标签: react-redux

我想在catch函数中从rest服务返回错误对象,但是它始终未定义

export const SignInUser = (username, password) => async dispatch => {
  try {
    let auth = {
      headers: {
        Authorization: 'Basic ' + Base64.btoa(username + ":" + password)
      }
    }
    const response = await axios.post("http://192.168.0.27:81/API/Auth/token", {}, auth);
    const {Username} = Base64.decoder(response.data)
    console.log(response);
    if(Username){
      dispatch({ type: AUTH_LOGIN, payload: {Username, IsAuthenticated:true} });
      Actions.Profile();
    }else{
      dispatch({ type: ERROR, payload: "Kullanici bilgileri bulunamadi" });
    }
  } catch (error) {
    // error object is undefined
    dispatch({ type: ERROR, payload: error });
    console.log(error)
  }
}

服务:

[Route("API/[controller]")]
    public class AuthController : Controller
    {
        [HttpPost("token")]
        public IActionResult Index()
        {
         throw new UnauthorizedAccessException("Invalid Username or password");
        }

如何从redux函数中的rest服务获取返回的数据(“无效的用户名或密码”)?

0 个答案:

没有答案