首页 > 解决方案 > 错误: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)
    });
  }, []);

有什么建议么?谢谢你:)

标签: mysqlnode.jsreactjssequelize.js

解决方案


您的 id 错误,它应该类似于 req.params.id 或 req.query.id ,具体取决于您使用的框架。


推荐阅读