Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/34.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 邮递员表单数据和x-www-Form编码返回空数据_Node.js_Mongodb_Rest_Express_Cart - Fatal编程技术网

Node.js 邮递员表单数据和x-www-Form编码返回空数据

Node.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

当I
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