CalebN99
CalebN99

Reputation: 99

Mongoose route isn't getting query data from axios function

Data being sent to the axios function isn't getting to the mongoose route in the back-end. The data is getting to the axios function from the front-end in react, but isn't making it to the back-end.

Front-end calling Axios

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

    this.props.getBugs(bugQuery);

Axios function

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

Mongoose route

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

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

Upvotes: 0

Views: 130

Answers (1)

Hakim Baheddi
Hakim Baheddi

Reputation: 287

You cannot use req.body in a GET method it's available just in POST, PUT and PATCH methods

update: or just use request params like this

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

        dispatch({ type: GET_BUGS, payload:   res.data, }) ); 
};

backend:

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"); 
});

Upvotes: 1

Related Questions