【发布时间】:2021-07-28 20:34:07
【问题描述】:
我正在处理一个帖子有很多 cmets 的项目,我正在尝试获取 cmets 的数量并将它们显示在 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)
});
}, []);
有什么建议吗? 谢谢:)
【问题讨论】:
-
您正在发出
get请求。试试req.params.id -
好的,我试过了,但它返回一个零 (0)
-
{cmetsCounter.id} 这是我渲染它以显示在 ui 中的位置
-
在发出请求(即
${id})之前,尝试控制台在前端记录您的idvar。 -
好的,这就是结果。 CommentsCounter 未定义
标签: mysql node.js reactjs sequelize.js