Route.post()需要回调函数,但得到了一个[object Object]

时间:2019-01-02 17:42:22

标签: node.js express backend middleware

我正在为express中的路由处理程序链接自定义中间件功能,但出现上述(标题)错误。这是为什么? 这是我的中间件代码:

const Joi = require("joi");

function validateCredentials(req, res, next) {
    const schema = {

      email: Joi.string()
        .max(1024)
        .required()
        .regex(/^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/),
      password: Joi.string()
        .min(6)
        .max(255)
        .required()
    };
  const result = Joi.validate({ email: req.body.email, password: req.body.password }, schema);
  if(!result.error) {
      return next();
  }
}

module.exports.validateCredentials = validateCredentials ;

这里是路由处理程序:

router.post('/api/signup', validateCredentials, passport.authenticate('local-signup'), (req, res) => {
    const response = {};
    response._id = req.user._id;
    response.email = req.user.local.email;
    res.send(response);
});

1 个答案:

答案 0 :(得分:0)

仅在验证通过时才调用next,而在出现错误时才调用。

您尝试过吗?

if (!result.error) {
  return next();
} else {
  return next(result.error);
}