javascript - 一个安静的 api 结束时的 ID 问题
问题描述
如果我只使用最后没有 /"some ID" 的 URL,则获取有效,但我想定位一个特定的 id。我认为问题出在客户端,因为router.patch()
与邮递员一起工作(使用相同的 URL)。但是我无法向服务器发送任何数据,因为 fetch 找不到 URL (404)。或者也许 fetch 不适用补丁请求,idk?
客户端:
async function update(){
const fromInp = { username: inp.value };
let param = {
method: 'PUT', // or 'POST'
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(fromInp),
}
await fetch(`http://localhost:8080/api/userModel/5e4ec9567566adade97df11e`, param)
}
服务器端:
router.patch('/:postId', async (req,res)=>{
try{
const update = await User.updateOne(
{_id: req.params.postId},
{ $set: {username: req.body.username}})
res.json(update)
} catch(err){
res.json({message: err})
}
})
解决方案
将方法属性设置为文本:'PATCH'
。
推荐阅读
- spring-boot - 在 Spring Boot 中解析 HTTP 请求标头时出错
- python - 组框内的组小部件
- javascript - .NET Core with Webpack 4 - how to access javascript module from razor view?
- javascript - 如何将变量字符串设置为对象选择器?
- angular - Angular 6 中的 Rxjs 导入问题
- database - 互连 IT 搜索替换数据库 HTTP 问题
- java - 无法从 Spring-boot SQL 查询中读取结果
- reflection - 有没有办法在 OCaml 中获取值的类型,如 c# 或 java 的反射?
- javascript - react-native-navigation v2:获取当前路线
- javascript - 无法在 ios 上模拟 Nativescript hello world