【问题标题】:Mongoose route isn't getting query data from axios functionMongoose 路由没有从 axios 函数获取查询数据
【发布时间】:2020-09-28 00:07:44
【问题描述】:

发送到 axios 函数的数据没有到达后端的 mongoose 路由。数据在 react 中从前端到达 axios 函数,但没有到达后端。

前端调用axios

    const bugQuery = {
      GroupID: "FRITOS",
    };

    this.props.getBugs(bugQuery);

axios函数

export const getBugs = (item) => (dispatch) => {
  console.log(item);
  axios.get("/api/bugs", item).then((res) =>
    dispatch({
      type: GET_BUGS,
      payload: res.data,
    })
  );
};

猫鼬路线

router.get("/", (req, res) => {
  console.log(req.body);
  Bugs.find({ GroupID: req.body.GroupID }).then((items) => res.json(items));

  console.log("Bugs loaded");
});

【问题讨论】:

    标签: javascript reactjs express mongoose axios


    【解决方案1】:

    您不能在 GET 方法中使用 req.body,它只能在 POST、PUT 和 PATCH 方法中使用

    更新: 或者只使用这样的请求参数

    export const getBugs = (item) => (dispatch) =>         { 
        console.log(item); 
        axios.get(`/api/bugs/${item.groupId}`).then((res) =>           
    
            dispatch({ type: GET_BUGS, payload:   res.data, }) ); 
    };
    

    后端:

    router.get("/api/bugs/:id", (req, res) => { 
       console.log(req.params.id);    
       Bugs.find({ GroupID: req.params.id }).then((items) => res.json(items)); console.log("Bugs loaded"); 
    });
    

    【讨论】:

    • 那我该如何访问方法中的数据呢?
    • 我刚刚更新了关于如何使用 req 参数的答案,希望它能拯救你
    猜你喜欢
    • 2016-11-24
    • 2013-09-21
    • 2021-01-17
    • 2018-10-17
    • 2019-08-15
    • 2023-01-12
    • 2014-08-12
    • 2022-09-29
    • 2016-06-18
    相关资源
    最近更新 更多