簡體   English   中英

UnhandledPromiseRejection:錯誤:Joi 的消息選項無效

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

我收到無效的錯誤消息。 我嘗試了很多東西,但沒有任何效果。 Joi 文檔 state 不推薦使用 Joi.validate 並且應該使用 schema.validate 但這對我也不起作用。Postman 只是掛起,直到我必須手動取消請求。 以下是發出創建用戶的發布請求時的代碼:

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

查看 out.validate 文檔以獲取更多信息

-- 這不是你應該如何捕捉驗證錯誤:

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

錯誤甚至不存在,應該是

if(validation.error) {
    
}

-- 你的嘗試應該放在 function 的頂部:

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

  }
})

-- 而且,我不確定您是否正確導出了registervalidation ,但它應該是:

module.exports = { registervalidation }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM