Javascript 未经处理的PromiseRejection:错误:Joi的消息选项无效

Javascript 未经处理的PromiseRejection:错误:Joi的消息选项无效,javascript,node.js,express,authentication,joi,Javascript,Node.js,Express,Authentication,Joi,我收到无效的错误消息。我试过很多东西,但都不管用。Joi文档声明Joi.validate已弃用,应使用schema.validate,但这对我也不起作用。邮递员只是挂起,直到我不得不手动取消请求。以下是创建用户的post请求时的代码: const { registervalidation } = require('../validation'); router.post('/register', async (req, res) => { //LETS VALIDATE cons

我收到无效的错误消息。我试过很多东西,但都不管用。Joi文档声明Joi.validate已弃用,应使用schema.validate,但这对我也不起作用。邮递员只是挂起,直到我不得不手动取消请求。以下是创建用户的post请求时的代码:

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);
module.exports = { registervalidation }

--这不是捕获验证错误的方式:

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,但应该是:

schema.validate(data);
module.exports = { registervalidation }

您的代码中有几个错误:

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

schema.validate(data, schema);
应该是:

schema.validate(data);
module.exports = { registervalidation }

--这不是捕获验证错误的方式:

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,但应该是:

schema.validate(data);
module.exports = { registervalidation }

您是否尝试将所有代码包装在try/catch中?尝试将您的Try移到顶部。我已经尝试过。但是错误仍然存在。您是否尝试将所有代码包装在Try/catch中?试着把你的尝试移到顶端。我已经试过了。但是错误仍然存在