Formdata не обновляет nodejs бэкэнд - PullRequest
0 голосов
/ 04 мая 2020

Я отправляю запрос на исправление с помощью ax ios, который выглядит следующим образом

const productData = new FormData();
    productData.append("ProductName", ProductName)
    productData.append("ProductCategory", ProductCategory)
    //productData.append("ProductImg", ProductImg, "ProductImg.jpg")
    for(var x = 0; x<ProductImg.length; x++) {
        productData.append('ProductImg', ProductImg[x])
    }
    axios.patch(`http://localhost:4500/products/${id}`, productData,
        {
            headers: {
                "Content-Type": "multipart/form-data",
                "Authorization": localStorage.getItem("Authorization")
            }
        }
        )
        .then(res => {
            console.log(res.data)

        })
        .catch(err =>
            console.log(`The error we're getting from the backend--->${err}`))

Я собираю данные через бэкэнд узла express следующим образом

exports.products_update_product = (req, res, next) => {
  const id = req.params.productId;
  const updateOps = {};
  for (const [key, value] of Object.entries(req.body)) {
    updateOps[key]= value
  }
  Product.update({ _id: id }, { $set: updateOps })
    .exec()
    .then(result => {
      res.status(200).json({
        message: "Product updated",
      });
    })
    .catch(err => {
      console.log(err);
      res.status(500).json({
        error: err
      });
    });
};

Проблема, с которой я здесь сталкиваюсь, заключается в том, что продукт не обновляется, а возвращает 200 из серверной части. Я не могу понять, что я делаю не так?

...