0
votes

For some reason the response is: TypeError: Cannot read property 'data' of undefined.

When I want to set the data in the cookie.

However when I just console log the data, it has no problems.

I've got the following request:

registerRoute.route("/register").post(async (req, res) => { const { username, password, email } = req.body;

try { await axios .post("url", { username: "###", email: "###", password: "###", }) .then((response) => { // Handle success. res.status(200); res.send("Registration complete");

    if (response.data.jwt) {
      res.cookie("secureCookie", response.data.jwt, {
        secure: process.env.NODE_ENV !== "development",
        httpOnly: true,
        expires: dayjs().add(30, "days").toDate(),
      });
    }
    console.log("User token", response.data.jwt);
  })
  .catch((error) => {
    // Handle error.
    res.status(400);
    if (error.response.data.message[0].messages[0].message) {
      res.send(error.response.data.message[0].messages[0].message);
    }
    console.log(
      "An error occurred:",
      error.response.data.message[0].messages[0].message
    );
  });

} catch (e) { res .status(400) .send("An error occurred:", e.response.data.message[0].messages[0]); } });

thank you!

1

1 Answers

0
votes

Found the problem! The cookie "expires" was faulty. The error just confused me because it said cannot read property 'data' of undefined. In the end it had nothing to do with 'data'.