1

I have an API which will send an email to a user based off the input. This is the on lick submit. I can confirm the data is being console.log for the state.

const inviteUser = async () => {
        userRequest.get(`companyprofile/companyUser/invite/${companyuser._id}`, teamMemberEmail);
        console.log('invite sent to', (teamMemberEmail))
    }

With this api, i send the body to the API and then email based off the text, however when i log the field it does not appear at all and only shows as {}

router.get("/companyUser/invite/:id", async (req, res) => {
  // must update to company
  var stringLength = 25;

  const companyUser = await CompanyProfile.findById(req.params.id)

  const companyUserToken = await companyUser.inviteToken
  const companyAccessTokenStripped = await companyUserToken.substring(0, stringLength);
  //encrypt the accesstoken to invite users

  const url = `http://localhost:5000/api/auth/inviteToJoinTeam/${companyUserToken}/${req.body.email}`;
  // const url = `http://localhost:5000/api/companyprofile/companyUser/inviteToJoinTeam/${companyUserToken}`;
  console.log(req.body)

  const  mailOptions = {
    from: 'company@gmail.com',
    to: req.body.email,
    subject: `You have been invited to join ${companyUser.CompanyTitle}`,
    html: `${companyUser.companyTitle} has invited you to join their team <a href="${url}">${url}</a>`
  };

  transporter.sendMail(mailOptions, function(error, info){
    if (error) {
      console.log(error);
    } else {
      console.log('Email sent: ' + info.response);
    }
  });


  try {
    // const savedCompany = await newCompany.save();
    res.status(201).json(companyUserToken);
  } catch (err) {
    res.status(500).json(err);
  }
});

Can anyone see why i cannot pass this data and email the user? Appears to be an error with how it's passed but i can confirm that the endpoint works in postman if i just plug an email body in

n_d22
  • 413
  • 4
  • 12

1 Answers1

0

You are trying to send an email whenever the GET route is called. However, the data won't be sent inside as "req.body" as a GET request doesn't have a body.

If you are using GET method then the data is sent as query parameters.

router.get("/companyUser/invite/:email", async (req, res, next) => {
   console.log(req.params.email);
};

You can also either use a POST or PUT request in order to access the URL body

router.post("/companyUser/invite", async (req, res, next) => {
   console.log(req.body.email);
};