Node.js 邮递员表单数据和x-www-Form编码返回空数据
当INode.js 邮递员表单数据和x-www-Form编码返回空数据,node.js,mongodb,rest,express,cart,Node.js,Mongodb,Rest,Express,Cart,当IPOST将Content\u Type设置为raw/JSON时,响应不为空。但是,当我尝试将Content\u Type设置为form data的POST数据时,响应返回为null。我怎样才能解决这个问题 但当我尝试使用原始数据/JSON时,没关系: 对于原始数据,我的内容类型为: multipart/form-data; boundary=<calculated when request is sent> 我的邮件路由器是这样的: router.post("/", upl
POST
将Content\u Type
设置为raw/JSON时,响应不为空。但是,当我尝试将Content\u Type
设置为form data的POST
数据时,响应返回为null。我怎样才能解决这个问题
但当我尝试使用原始数据/JSON时,没关系:
对于原始数据,我的内容类型为:
multipart/form-data; boundary=<calculated when request is sent>
我的邮件路由器是这样的:
router.post("/", upload.single("productImage"), (req, res, next) => {
console.log(req.file);
Category.findById(req.body.categoryID)
.then((category) => {
if (!category) {
return res.status(404).json({
message: "Category not found",
category: category,
});
}
//
const product = new Product({
_id: new mongoose.Types.ObjectId(),
name: req.body.name,
price: req.body.price,
quantity: req.body.quantity,
category: req.body.categoryID,
});
return product.save();
})
.then((result) => {
console.log(result);
res.status(201).json({
message: "Object created succesfully",
product: {
id: result._id,
name: result.name,
price: result.price,
quantity: result.quantity,
category: result.category,
},
});
})
.catch((err) => {
console.log(err), res.status(500).json({ error: err });
});
});
错误是:
UnhandledPromiseRejectionWarning: Error
[ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the
client
如何解决此问题?您在表单数据中以错误的方式写入var示例:正确的方式类别ID而不是类别您在表单数据中以错误的方式写入var示例:正确的方式类别ID您是否能够解决此问题?即使我也有同样的问题你能解决这个问题吗?就连我也有同样的问题
UnhandledPromiseRejectionWarning: Error
[ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the
client