简体   繁体   English

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

[英]UnhandledPromiseRejection: Error: Invalid message options with Joi

I am getting Invalid error messages.我收到无效的错误消息。 I have tried many things but nothing is working.我尝试了很多东西,但没有任何效果。 The Joi docs state that Joi.validate is deprecated and schema.validate should be used but that is not working for me either.Postman just hangs until I have to cancel the request manually. Joi 文档 state 不推荐使用 Joi.validate 并且应该使用 schema.validate 但这对我也不起作用。Postman 只是挂起,直到我必须手动取消请求。 Below is the code when making a post request to create a user:以下是发出创建用户的发布请求时的代码:

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

The joi schema looks like these.. 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);   

};

It throws error..它抛出错误..

(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.

You have several errors in your code:您的代码中有几个错误:

-- You are not validating your schema correctly: -- 您没有正确验证您的架构:

schema.validate(data, schema);

it should be:它应该是:

schema.validate(data);

check out.validate documentation for more information 查看 out.validate 文档以获取更多信息

-- This it not how you should catch the validation error: -- 这不是你应该如何捕捉验证错误:

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

error doesn't even exist, it should be错误甚至不存在,应该是

if(validation.error) {
    
}

-- Your try should be placed at the top of your function: -- 你的尝试应该放在 function 的顶部:

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

  }
})

-- And, I am not sure if you are exporting your registervalidation correctly, but it should be: -- 而且,我不确定您是否正确导出了registervalidation ,但它应该是:

module.exports = { registervalidation }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM