Home > Enterprise >  Formdata not being sent through to NodeJS backend
Formdata not being sent through to NodeJS backend

Time:05-08

I have a function that should send FormData as an axios.post call to my api. It gets activated with a press of a button

  const sendCard = (card) => {
    debugger;
    let data = new FormData();
    data.append("itemID", itemID);
    data.append("comment", comment);
    data.append("dateStart", dates[0]);
    data.append("dateEnd", dates[1]);
    data.append("qtyWant", qtyWant);
    requestBooking(data);
  };

and here is the axios part:

export const requestBooking = async (data) => {
  return await api
    .post(`/booking/request`, data)
    .then((data) => {
      return "DONE!";
    })
    .catch((err) => {
      console.log(err.response.data);
    });
};

the api is an import from a config.js file, and everything there works with all the other aspects of my website.

I have another FormData object being posted into the api, and that one works properly, even though there is no differences at all...

Here is the accepting function on the back end

  app.post(
    "/api/booking/request",
    request
  );

exports.request = (req, res) => {
  res.status(200).send("Reached");
};

where req.body is just an empty object ({})

This all seems like it should work. And I don't know what am I missing

CodePudding user response:

the route used in axios is not the same as the server route

CodePudding user response:

The sendCard function is should be async as well and you should include an await before requestBooking.

 const sendCard = async (card) => {
    debugger;
    let data = new FormData();
    data.append("itemID", itemID);
    data.append("comment", comment);
    data.append("dateStart", dates[0]);
    data.append("dateEnd", dates[1]);
    data.append("qtyWant", qtyWant);
    await requestBooking(data);
  };
  • Related