javascript - 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);
}
解决方案
推荐阅读
- java - 选择项目后项目图标不会显示
- entity-framework-core - Nuget - ASP.NET CORE 的实体框架核心问题 - MVC)
- python - Python Seaborn:如何绘制所有列并将索引用作色调?
- integration-testing - Maven 验证不执行具有故障安全功能的 Cucumber-Serenity 集成测试
- django - mw_instance = middleware(adapted_handler) TypeError: __init__() 接受 1 个位置参数,但给出了 2 个
- javascript - 在两个函数之间传递道具会产生错误(道具未定义)
- reactjs - npm 错误!代码 1.为什么这个错误消息会发生 npm ERR!代码1?我该如何解决
- sql - 在 Django Rest Framework 的视图中发生的 SQL 连接
- c++ - IDeviceTopology如何激活devicetopology.h的接口?
- express - ExpressJs - Mongoose:删除具有多对多关系的文档