如果我只使用末尾不带/“某个ID”的URL,则提取有效,但我想定位一个特定的ID。我认为问题出在客户端,因为 router.patch()
与 postman 一起工作(使用相同的 URL)。但是我无法向服务器发送任何数据,因为获取无法找到 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'
。
关于javascript - Restful api 末尾的 Id 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60340281/