javascript - UnhandledPromiseRejection: Error: Invalid message options with Joi - Stack Overflow

I am getting Invalid error messages. I have tried many things but nothing is working. The Joi docs stat

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

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: ['', '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.exportspile (D:\Projects\web\jwt\node_modules\joi\lib\messages.js:30:5)
at Object.exports.preferences (D:\Projects\web\jwt\node_modules\joi\lib\mon.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 
 ` (see 
.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.

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

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: ['', '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.pile (D:\Projects\web\jwt\node_modules\joi\lib\messages.js:30:5)
at Object.exports.preferences (D:\Projects\web\jwt\node_modules\joi\lib\mon.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/apejections=strict` (see 
https://nodejs/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.
Share Improve this question edited Mar 16, 2021 at 20:38 TheLazyPandey asked Mar 16, 2021 at 20:31 TheLazyPandeyTheLazyPandey 521 silver badge7 bronze badges 2
  • Did you try wrapping all your code inside the try/catch? Try to move your try to the top. – soltex Commented Mar 17, 2021 at 9:26
  • I have tried that..but the error remains – TheLazyPandey Commented Mar 18, 2021 at 8:13
Add a ment  | 

1 Answer 1

Reset to default 6

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

-- 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:

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:

module.exports = { registervalidation }

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745610972a4635946.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信