Shayane
Shayane

Reputation: 33

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

I am using redux toolkit and doing API call. However I am getting the error: Uncaught (in promise) TypeError: Cannot read property 'status' of undefined at SignUpForm.otpEmailCallBack (signUp.jsx:215) at 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} />);
    }
  };

Any help is appreciated.

Upvotes: 1

Views: 3356

Answers (1)

Shubham Khatri
Shubham Khatri

Reputation: 282080

You are not passing the res from api to otpEmailCallback which is why it receives the values as undefined when it is being called. To handle that you can bind the response to the function when you are passing it as callback to sendEmailOtp function

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

Upvotes: 1

Related Questions