title: How to cancel a scheduled mail in SendGrid in nodeJS?
body: <p>I'm working on a React app for a user to create reminders, and when the reminder is created, an e-mail will be sent to the user.
  my problem is that if the usur set the date to a later time and then wants to delete the reminder, I need the mail to be cancelled.
  I looked through sendgrid docs, and just couldn't make it work. i always get an error saying 'ResponseError: Unauthorized'.</p>
  <p>So i'd like to know how to do that... Thanks!</p>
  <p>I'm a beginner so sorry if the question is a little silly...</p>
  <p>Here is how I post the reminder to DB and send a mail to the user.</p>
  <pre><code>router.post(&quot;/save&quot;, (req, res) =&gt; {
  console.log(&quot;Body: &quot;, req.body);
  
  const data = req.body;
  
  const newReminder = new Reminder(data);
  
  newReminder.save((err) =&gt; {
  if (err) {
    res.status(500).json({ msg: &quot;Sorry, internal server errors.&quot; });
  } else {
    const API_KEY = process.env.API_KEY_SENDGRID;
    sgMail.setApiKey(API_KEY);
  
    User.findOne({ username: data.username }, (err, foundUser) =&gt; {
      if (!err) {
        const year = new Date();
        ejs.renderFile(
          &quot;views/index.ejs&quot;,
          { title: data.title, body: data.body, year: year.getFullYear() },
          function (err, page) {
            if (err) {
              console.log(err);
            } else {
              const date = `${data.date} ${data.time}`;
              const dateTime = new Date(date);
              const unixTimeStamp = Math.floor(dateTime.getTime() / 1000);
  
              const message = {
                to: foundUser.email,
                from: {
                  name: appName,
                  email: appMail,
                },
                subject: data.title,
                html: page,
                send_at: unixTimeStamp,
              };
              sgMail
                .send(message)
                .then((response) =&gt; {
                  console.log(message);
                  console.log(&quot;Email sent...&quot;);
                })
                .catch((error) =&gt; console.log(error.message));
            }
          }
        );
      }
    });
  }
  });
  
  res.json({
    msg: &quot;We received your data!&quot;,
  });
  });
  </code></pre>
  <p>And this is how I delete a reminder</p>
  <pre><code>router.delete(&quot;/delete&quot;, (req, res) =&gt; {
  const data = req.body.id;
  Reminder.findByIdAndRemove(data, (err) =&gt; {
  if (!err) {
    res.send(&quot;wrks&quot;);
  } else {
    console.log(err);
  }
  });
  });
  </code></pre>