I can't figure out why I'm getting this response from express-validator:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Error</title>
</head>
<body>
<pre>Cannot POST /product</pre>
</body>
</html>
I'm receiving a formdata request on my server and I'm extracting the file image and the text fields as expected. Then I'm adding the fields extracted to the body of the request object. Then I call the express-validator function to validate the fields. Until this point everything works fine, if I send wrong data to the server, my validator replies with the fields that aren't correct, but if all the fields are correct I get the response mentioned before and I can't figure out why.
This is my validator function:
fieldProduct(req, res, next) {
req.assert('name', 'Should not be empty').notEmpty();
req.assert('name', 'Should have 2 to 40 characters').len(2, 40);
req.assert('price', 'Should not be empty').notEmpty();
req.assert('price', 'Should be numeric and like this: 12,12 or 1.10').matches(/^[\d]+[\.]{1}[\d]{1,2}/g);
req.assert('categoryId', 'Should not be empty').notEmpty();
req.assert('categoryId', 'Should have 10 caracters').len(10, 10);
var errors = req.validationErrors();
errors ? res.status(400).json(errors) : next();
}
And this is the function where I call the validator after I have constructed the body of the request with the fields I had extracted from my formdata:
product.create = function(req, res, next, callback) {
console.log("DAO create product ");
Crud.upload(req, function(status, data, body) {
if(status == 200) {
req.body.name = body.name[0];
req.body.brand = body.brand[0];
req.body.description = body.description[0];
req.body.categoryId = body.categoryId[0];
req.body.price = Number(body.price[0]);
console.log(req.body);
validate.fieldProduct(req, res, next);
if(!req.validationErrors()) {
var object = req.body;
object.imageFile = data;
Crud.create(object, Product, constructProduct, function(status, data) {
callback(status, data);
});
} else {
res.status(400).json(req.validationErrors())
}
} else {
callback(status, data);
}
});
}
I also noticed that besides I get that response, my code keeps running and in the database the data is created as expected.