问题
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