express server doesn't start after adding checkSchema from express-validator

早过忘川 提交于 2020-04-30 07:03:38

问题


I'm working on an old express server, I was using check from express-validator and it works fine. But I have a post request where the body fields are nested and I need to validate them, so I'm trying to use checkSchema. Apparently its included in the new version, so i upgraded express and express-validator

"express": "^4.17.1",

"express-validator": "^6.4.0",

But when I added checkSchema to my function (code below), the server does not start anymore. When i comment it, it works fine.

my import

const {check, validationResult, checkSchema } = require('express-validator');

my function

app.post('/updateuser', 
checkSchema({
  payload:{
    profile:{
      age:{
        in:[body],
        errorMessage: 'age must be an integer',
        isInt: true,
      }
    }
  }
}),
async (req, res) => {
  Object.assign(log, { header: req.headers, body: req.body, query: req.query, params: req.params })
  const errors = validationResult(req);
  if (!errors.isEmpty()) {
    return res.status(422).json({ errors: errors.array() });
  }
  await request(uri, UPDATE, {
    payload: JSON.stringify(req.body.payload)
  }).then(result => {
    res.status(200).send({ "updateuser": JSON.parse(result.updateUserData).msg });
  }).catch(error => {
    res.status(500).end();
  });
})

Could anyone tell me what is wrong here ?

来源:https://stackoverflow.com/questions/61292782/express-server-doesnt-start-after-adding-checkschema-from-express-validator

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!