无法 POST 使用 formdata 在 express-validator 响应
Cannot POST response at express-validator using formdata
我不明白为什么我会收到 express-validator 的回复:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Error</title>
</head>
<body>
<pre>Cannot POST /product</pre>
</body>
</html>
我在我的服务器上收到一个表单数据请求,我正在按预期提取文件图像和文本字段。然后我将提取的字段添加到请求对象的主体中。然后我调用 express-validator 函数来验证字段。在此之前一切正常,如果我向服务器发送错误数据,我的验证器会回复不正确的字段,但如果所有字段都正确,我会得到前面提到的响应,但我不知道为什么。
这是我的验证器函数:
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();
}
这是我在使用从表单数据中提取的字段构建请求主体后调用验证器的函数:
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);
}
});
}
我还注意到,除了收到该响应外,我的代码还保持 运行 并且在数据库中按预期创建了数据。
您看到的消息是 Express 在调用 next()
时返回的默认错误消息,但链中没有其他路由或中间件处理程序。我不认为这是验证器的问题。
您是否有应该为此请求发送响应的代码?
我建议添加一个通用的 404 处理程序来确认这是发生了什么:https://expressjs.com/en/starter/faq.html#how-do-i-handle-404-responses
我不明白为什么我会收到 express-validator 的回复:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Error</title>
</head>
<body>
<pre>Cannot POST /product</pre>
</body>
</html>
我在我的服务器上收到一个表单数据请求,我正在按预期提取文件图像和文本字段。然后我将提取的字段添加到请求对象的主体中。然后我调用 express-validator 函数来验证字段。在此之前一切正常,如果我向服务器发送错误数据,我的验证器会回复不正确的字段,但如果所有字段都正确,我会得到前面提到的响应,但我不知道为什么。
这是我的验证器函数:
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();
}
这是我在使用从表单数据中提取的字段构建请求主体后调用验证器的函数:
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);
}
});
}
我还注意到,除了收到该响应外,我的代码还保持 运行 并且在数据库中按预期创建了数据。
您看到的消息是 Express 在调用 next()
时返回的默认错误消息,但链中没有其他路由或中间件处理程序。我不认为这是验证器的问题。
您是否有应该为此请求发送响应的代码?
我建议添加一个通用的 404 处理程序来确认这是发生了什么:https://expressjs.com/en/starter/faq.html#how-do-i-handle-404-responses