首页 > 解决方案 > Fetch API PUT 没有找到管道

问题描述

我尝试使用 Fetch API PUT 更新我的 JSON 上的一些数据,但它说在控制台中找不到 404。当我在 POSTMAN 中对其进行测试时,它还可以(200)。我怀疑 fetch 有什么问题。

这里是获取代码。

fetch(`/api/customers/21111`, {
      method: "PUT",
      headers: {
        Accept: "aplication/json",
        "Content-Type": "aplication/json"
      },
      body: JSON.stringify({
        Variabel: "coba"
      })
    }).catch(function(error) {
      console.log(
        "There has been a problem with your fetch operation: ",
        error.message
      );
    });
    };

这里是快递代码。

app.put("/api/customers/:id", (req, res) => {
const customer = customers.find(c => c.id === parseInt(req.params.id));
if (!customer) res.status(404).send("Id is not found");

const { error } = validateCourse(req.body);
if (error) {
  res.status(404).send(error.details[0].message);
  return;
}

customer.Variabel = req.body.Variabel;
res.send(customer);
});
function validateCourse(customer) {
const schema = {
  Variabel: Joi.string()
    .min(3)
    .required()
};
return Joi.validate(customer, schema);
}

标签: javascriptreactjsexpress

解决方案


推荐阅读