最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

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

matteradmin4PV0评论

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 }
Post a comment

comment list (0)

  1. No comments so far