UnhandledPromiseRejection:错误:Joi 的消息选项无效

问题描述

我收到无效的错误消息。我尝试了很多东西,但没有任何效果。 Joi 文档声明不推荐使用 Joi.validate 并且应该使用 schema.validate 但这对我也不起作用。邮递员只是挂起,直到我必须手动取消请求。以下是发出帖子请求以创建用户时的代码

const { registervalidation } = require('../validation');

router.post('/register',async (req,res) => {

  //LETS VALIDATE
 const validation = registervalidation(req.body);
 if(error) return res.status(400).send(error.details);
 
const users = new User ({
    name: req.body.name,email: req.body.email,password: req.body.password 
 });

try{
    const usersave = await users.save();
    res.send(usersave);
}
catch(err){
    res.status(400).send(err);
    console.log(err);
}
});

joi 架构看起来像这些..

const Joi = require('joi');

const registervalidation = data =>{

const schema = Joi.object({
    name:     Joi.string()
                 .alphanum()
                 .min(6)
                 .max(30)
                 .required()
                 .error(new Error('I am a custom error and I kNow it!')),email:     Joi.string()
                 .email({minDomainSegments: 2,tlds: { allow: ['com','net'] } })
                 .required()
                 .error(new Error('I am a custom error and I kNow it!')),password:  Joi.string()
                 .min(6)
                 .required()
                 .error(new Error('I am a custom error and I kNow it!'))
})
return schema.validate(data,schema);   

};

它抛出错误..

(node:17752) UnhandledPromiseRejectionWarning: Error: Invalid message options
at new module.exports (D:\Projects\web\jwt\node_modules\@hapi\hoek\lib\error.js:23:19)
at module.exports (D:\Projects\web\jwt\node_modules\@hapi\hoek\lib\assert.js:20:11)
at Object.exports.compile (D:\Projects\web\jwt\node_modules\joi\lib\messages.js:30:5)
at Object.exports.preferences (D:\Projects\web\jwt\node_modules\joi\lib\common.js:165:36)
at Object.exports.entry (D:\Projects\web\jwt\node_modules\joi\lib\validator.js:24:27)
at internals.Base.validate (D:\Projects\web\jwt\node_modules\joi\lib\base.js:548:26)
at registervalidation (D:\Projects\web\jwt\validation.js:23:19)
at D:\Projects\web\jwt\routes\auth.js:9:25
at Layer.handle [as handle_request] (D:\Projects\web\jwt\node_modules\express\lib\router\layer.js:95:5)
at next (D:\Projects\web\jwt\node_modules\express\lib\router\route.js:137:13)
at Route.dispatch (D:\Projects\web\jwt\node_modules\express\lib\router\route.js:112:3)
at Layer.handle [as handle_request] (D:\Projects\web\jwt\node_modules\express\lib\router\layer.js:95:5)
at D:\Projects\web\jwt\node_modules\express\lib\router\index.js:281:22
at Function.process_params (D:\Projects\web\jwt\node_modules\express\lib\router\index.js:335:12)
at next (D:\Projects\web\jwt\node_modules\express\lib\router\index.js:275:10)
at Function.handle (D:\Projects\web\jwt\node_modules\express\lib\router\index.js:174:3)
 (node:17752) UnhandledPromiseRejectionWarning: Unhandled promise rejection. 
 This error originated either by throwing inside of an async function without 
 a catch block,or by rejecting lock,or by rejecting a promise which was not 
handled with .catch(). To terminate the node process on unhandled promise 
rejection,use the CLI flag `--unhandled-r 
 https://nodejs.org/apejections=strict` (see 
https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection 
id: 1)
(node:17752) [DEP0018] DeprecationWarning: Unhandled promise rejections are 
 deprecated. In the future,promise rejections that are not handled will 
 terminate 
 the Node.js process with a
 Node.js process with a non-zero exit code.

解决方法

您的代码中有几个错误:

-- 您没有正确验证您的架构:

schema.validate(data,schema);

应该是:

schema.validate(data);

check out .validate documentation for more information

-- 这不是您应该如何捕获验证错误:

const validation = registervalidation(req.body);
if(error) return res.status(400).send(error.details);

错误甚至不存在,应该是

if(validation.error) {
    
}

-- 您的 try 应该放在函数的顶部:

router.post('/register',async (req,res) => {
  try {
  
  } catch (err) {

  }
})

-- 而且,我不确定您是否正确导出了registervalidation,但应该是:

module.exports = { registervalidation }