首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >UnhandledPromiseRejection:错误: Joi的无效消息选项

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

Stack Overflow用户
提问于 2021-03-16 20:31:31
回答 1查看 2.3K关注 0票数 0

我收到的错误信息无效。我试过很多东西,但都没有用。Joi声明不推荐使用Joi.validate,应该使用schema.validate,但这对我不起作用,either.Postman只是挂起,直到我不得不手动取消请求。下面是创建用户的post请求时的代码:

代码语言:javascript
运行
复制
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模式看起来如下..。

代码语言:javascript
运行
复制
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);   

};

它抛出错误..。

代码语言:javascript
运行
复制
(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.
EN

Stack Overflow用户

回答已采纳

发布于 2021-03-18 18:02:10

您的代码中有几个错误:

-您没有正确地验证您的模式:

代码语言:javascript
运行
复制
schema.validate(data, schema);

它应该是:

代码语言:javascript
运行
复制
schema.validate(data);

有关更多信息,请查看.validate文档

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

代码语言:javascript
运行
复制
const validation = registervalidation(req.body);
if(error) return res.status(400).send(error.details);

error甚至不存在,应该是

代码语言:javascript
运行
复制
if(validation.error) {
    
}

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

代码语言:javascript
运行
复制
router.post('/register', async (req, res) => {
  try {
  
  } catch (err) {

  }
})

-而且,我不确定你是否正确地导出了你的registervalidation,但它应该是:

代码语言:javascript
运行
复制
module.exports = { registervalidation }
票数 4
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66662971

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档