express - 为什么在向空路由发出 PUT 请求时出现 Mongoose 错误?
问题描述
我有这个功能,它使 axios 提出请求/api/lyric/tag
const addTag = async (name, tagId, lyricId) => {
const body = {name, tagId, lyricId};
await axios.put(`/api/lyric/tag`, JSON.stringify(body), { headers: {'Content-Type': 'application/json'} });
}
目前该路线/api/lyric/tag
完全是空的:
// @route PUT api/lyric/tag
// @desc Add tag to lyric
// @access Private
router.put('/tag', auth, async (req, res) => {
});
然而由于某种原因,每次 axios 发出 put 请求时我都会收到此错误:
Cast to ObjectId failed for value "tag" at path "_id" for model "lyric"
我失去理智了吗?
解决方案
将 res.send("OK") 或 res.json({ message: "OK" }) 放在路由的末尾。
您收到此错误是因为您没有响应请求者。
或者这条路线正在匹配另一条路线......
/api/lyric 有什么路线吗?如果它有,在某种程度上等于 /api/lyric/:_id,它就会被调用。
尝试将路由更改为 /api/lyric/tag/update。它不是 REST,但您的路线将被调用。然后,您将需要重构 api 以使其具有凝聚力。
推荐阅读
- mysql - 如何只计算左连接中的记录?
- php - PHP .htaccess 重写 url
- gradle - Meteor 离线构建错误
- java - Java - Selenium ChromeDriver - “Chrome 自动化扩展已崩溃”
- nginx - 在没有 sudo 权限的情况下使用我的 UserId 重新启动/停止 Nginx
- sql - PostgreSQL,有一个列作为求和函数
- javascript - 带有溢出的点击容器 div:自动
- reactjs - Storybook addDecorator 不适用于 config.js?
- bash - Bash 循环没有正确更新输出
- php - 如何在电报机器人中传递键盘的动态按钮值?