未处理的拒绝 (TypeError):无法读取未定义的 属性 'status'

Unhandled Rejection (TypeError): Cannot read property 'status' of undefined

我正在使用 redux 工具包并进行 API 调用。但是我收到错误: 未捕获(承诺)TypeError:无法读取未定义的 属性 'status' 在 SignUpForm.otpEmailCallBack (signUp.jsx:215) 在 api.js:50

otpEmailCallBack = (res) => {
    if (res.status === 200) {
      toast(<AlertSuccess message={res.data.otp} />);
      this.props.updateStep(null, 4);
      this.props.updateVerifyEmail({
        otp: res.data.otp,
        email: this.state.data.email,
        code: res.data.code,
      });
    } else {
      toast(<AlertError message={res.data.error} />);
    }
  };

感谢任何帮助。

您没有将 res 从 api 传递到 otpEmailCallback,这就是为什么它在被调用时收到未定义值的原因。要处理这个问题,您可以在将响应作为回调传递给 sendEmailOtp 函数时将响应绑定到函数

emailCallback = (res) => {
    if (res.status === 200) {
        const emailParams = {
          email: this.state.data.email
        };
        this.props.sendEmailOtp(emailParams, this.otpEmailCallBack.bind(this, res));
    } else {
        this.setState({
          loader: false,
          btnClass: "btn btn-lg btn-primary btn-block",
        });
        toast( < AlertError message = {
              res.data.error
            }
            />);
     } 
 };