首页 > 解决方案 > 一个安静的 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})
                }
            })

标签: javascriptnode.jsmongodb

解决方案


将方法属性设置为文本:'PATCH'


推荐阅读