mysql - 错误:WHERE 参数“id”具有无效的“未定义”值,reactjs
问题描述
我正在开发一个帖子有很多评论的项目,我正在尝试获取评论的数量并将它们显示在 UI 中。
但我收到错误
Error: WHERE parameter "id" has invalid "undefined" value
这是服务器端:
router.get("/commentsCounter/:id", async (req, res) => {
const PostId = req.body.PostId;
const commentsCounter = await Comments.count({
where:{
id: PostId,
}
});
res.json({ commentsCounter });
console.log("commentsCounter", commentsCounter);
});
这是客户端:
useEffect((PostId) => {
axios.get(`http://localhost:3007/comments/commentsCounter/${id}`).then((res) => {
console.log('setCommentsCounter', res.data.commentsCounter)
setCommentsCounter(res.data.commentsCounter)
});
}, []);
有什么建议么?谢谢你:)
解决方案
您的 id 错误,它应该类似于 req.params.id 或 req.query.id ,具体取决于您使用的框架。
推荐阅读
- php - 文本行匹配框的透明背景颜色(填充颜色)
- javascript - 为什么是 Object.assign() 而不是 this.assign() (对于对象)?
- vba - 为什么我的 VBA 宏只拆分 Word 文档的第一和第三部分?
- python - 如何修复/解决 sympy dsolve 函数中的 NotImplemented 错误?
- sapui5 - 有没有办法在概览页面的条形字符列表卡中显示超过 5 个项目?
- vba - 在 Access 表单中,有没有办法使用搜索框使用 VBA 填充表单?
- javascript - 如何在excel中绑定组合框添加使用office js
- python - 在 Python 中使用 ACL 检查、编辑、删除文件或文件夹权限
- python - 如何使用 sqlalchemy 将具有 datetime.timedelta 类型数据对象的 pandas 数据帧存储到 postgresql d/b 中?
- python-3.x - 使用 sslmode 运行 postgres 连接字符串(用于 SQLAlchemy)时出现 Docker 错误