我在我的反应和休息 api 应用程序中遇到错误,其中我的一个变量返回未定义

Im getting an error in my react and rest api app where one of my variables is coming back undefined

如果您正在阅读本文,感谢您的宝贵时间,我正在制作一个具有反应和休息功能的全栈应用程序 api,但我遇到了这样的错误:

TypeError: errors is undefined
    submit UserSignUp.js:53
    promise callback*UserSignUp/this.submit UserSignUp.js:51
    React 17
    unstable_runWithPriority scheduler.development.js:653
    React 24
    js index.js:8
    Webpack 7

抛出此错误的部分就在这里:

context.userController.createUser(user)
    .then( errors => {
      if (errors.length) {
        this.setState( {errors});
      } else {
        context.actions.signIn(emailAddress,password)
        .then( () => {this.props.history.push('/authenticated')}  );
      }
      })
    .catch( err => { // handle rejected promises
      console.log(err);
      this.props.history.push('/error'); // push to history stack
    })
  }
}

这是我的 createUser 函数代码:

async createUser(user){
    const response = await this.call_api('/users', 'POST', user);
    if (response.status === 201) {
      return [];
    }
    else if (response.status === 400) {
      return response.json().then(data => {
        return data.errors;
      });
    }
    else {
      throw new Error();
    }
  }
  }

这里是代码处理 post / users

router.post('/users', asyncHandler(async (req, res, next) => {
    const user = req.body;

    if(user.password)
    {
        user.password = bcryptjs.hashSync(user.password);
    };

    try
    {
        await User.create(user);
        res.status(201).location('/').end();
    } catch (error) {
        if (error.name === 'SequelizeValidationError' || error.name === 'SequelizeUniqueConstraintError')
     {
            const errorMsg = [];

            error.errors.map((err) => errorMsg.push(err.message));
            res.status(400).json({ error: errorMsg });
        } else
        {
            next(error);
        };
    };
}));

我真的不确定这里的错误变量是怎么回事,所以如果有人能帮助我,那就太好了,我的 github link: https://github.com/zakMossy/react-and-rest-api-project-10

如果响应状态不是 201,您的 createUser 函数代码 returns data.errors。但是您的 API 在 error 键中发送错误。在您的 API 路由处理程序中将密钥更新为 errors,您应该可以开始了。

 res.status(400).json({ errors: errorMsg });