无法读取未定义的属性“msg”

问题描述 投票:0回答:1
 const onSubmit = async () => {
    showError(false)

    try {
      await axios.post("http://localhost:9005/api/Users", user);
      // history.push("/")
      handleClose()
      props.snackbarShowMessage(`User Added Successfully `)
      data()
      formReset()
    } catch(error) {
      showError(true)
      console.log('asdfg',error)
      props.snackbarShowError(error.response.data.error[0].msg)
    }
  };
reactjs
1个回答
0
投票

将响应分配给变量,然后检查其响应状态。 您可以使用...来验证这一点

const x =  await axios.post("http://localhost:9005/api/Users", user);

console.log(x.statusCode);

请注意,try catch 将处理反应错误而不是 axios 错误..

© www.soinside.com 2019 - 2024. All rights reserved.