node.jssendgridunhandled

Problem while dealing with sendgrid API with Node.js


app.post('/users/update',async(req,res)=>{
    try{
        const user = await User.findOne({email:req.body.email});
        if(!user)
        {
            return res.send({message:"Email ID not registered!"})
        }
        // SG.K_2ita5rRLWvBO6ind1FzQ.uXUAMa1IzOHV155Sak74blUlmawGeUeRXQ20O5tFA
        sgMail.setApiKey('SG.K_2ita5rRLWvBO6ind1FzQ.uXUAMa1IzOHV155Sak74blUlmawGeUeRXQ20O5tFA');
        const msg = {
            to: user.email,
            from: 'kartiken@gmail.com',
            subject: 'Validate Your Account',
            text: 'Hello User, here is your OTP: ',
            html: '<strong>21345</strong>',
            };
        sgMail.send(msg)
        .then(()=>{res.render('forgotPass2',{user})})
        .catch((e)=>{console.log('error',e)})
        console.log('Check mail')

    }catch(e){
        res.status(400).send(e)
    }
})

I am really tired of searching for a solution to this problem. The mail is never sent and I keep getting an error everytime after whatever I do. The error is caught by the promise.. I am attaching the error here!

Check mail
error ResponseError: Forbidden
    at C:\Users\Niranjan\Desktop\krb\web d\node\login-system\node_modules\@sendgrid\client\src\classes\client.js:133:29
    at processTicksAndRejections (internal/process/task_queues.js:97:5) {
  code: 403,
  response: {
    headers: {
      server: 'nginx',
      date: 'Tue, 14 Jul 2020 20:28:08 GMT',
      'content-type': 'application/json',
      'content-length': '281',
      connection: 'close',
      'access-control-allow-origin': 'https://sendgrid.api-docs.io',
      'access-control-allow-methods': 'POST',
      'access-control-allow-headers': 'Authorization, Content-Type, On-behalf-of, x-sg-elas-acl',
      'access-control-max-age': '600',
      'x-no-cors-reason': 'https://sendgrid.com/docs/Classroom/Basics/API/cors.html'
    },
    body: { errors: [Array] }
  }
}

Pls help me with this so that I can move further...


Solution

  • from this link in documentation says:

    https://github.com/sendgrid/sendgrid-nodejs/tree/master/packages/mail

    Verify Sender Identity

    "Verify an email address or domain in the Sender Authentication tab. Without this you will receive a 403 Forbidden response when trying to send mail."

    after you logged into your account

    https://app.sendgrid.com/settings/sender_auth/senders

    and btw code looks fine