StephenGrider / FullstackReactCode

Companion repo to https://www.udemy.com/node-with-react-fullstack-web-development
1.39k stars 1.17k forks source link

UnhandledPromiseRejectionWarning: SendGridError: Response error ; Layman Fix #75

Open vinc3nati opened 4 years ago

vinc3nati commented 4 years ago

In the new version of sendgrid , we have to 1. Verify our account and 2. Single Sender Authentication . The mail which you specify in the Single Sender Authentication must be the same in the

.fromEmail attribute

We cannot specify an email unless it is genuine i.e. we cannot use 'no-reply@emaily.com' unless it is a verified email.

ganya7 commented 4 years ago

Hey, can you provide the steps for the fix and what code have you used for it? I have to use the updated library right ie @sendgrid/mail and the email has to be single sender authentication should be used in the code as well right?

vinc3nati commented 4 years ago

Hey, can you provide the steps for the fix and what code have you used for it? I have to use the updated library right ie @sendgrid/mail and the email has to be single sender authentication should be used in the code as well right?

I have created a temporary Email Id for sending emails and updated the same in both Sendgrid Sender Authentication and from field in our code. It works fine then :D

ganya7 commented 4 years ago

yeah I was able to get it work. Anyone who is still facing the issue here is the way it worked for me: you have to download and use the new sengrid library: @sendgrid/mail ie npm install and save it in package.json you have to do the sendgrid single sender authentication step which is going to be your "from" email and use the following code:

Mailer.js

const keys = require('../config/keys');
const sgMail = require('@sendgrid/mail');

module.exports = async ({ subject, recipients }, content) => {
  // using SendGrid's v3 Node.js Library
  // https://github.com/sendgrid/sendgrid-nodejs
  sgMail.setApiKey(keys.sendGridKey);
  const formattedRecipients = recipients.map(({email}) => email);
  const msg = {
    to: formattedRecipients,
    from: 'someone@gmail.com',
    subject: subject,
    html: content,
  };
  return await sgMail.send(msg);
}

surveyRoutes.js

const m = Mailer(survey, surveyTemplate(survey));

ganya7 commented 4 years ago

Remember while testing it out, on the dev tools console you might get an error like:

xhr.js:178 POST http://localhost:3000/api/surveys net::ERR_EMPTY_RESPONSE createError.js:16 Uncaught (in promise) Error: Network Error at createError (createError.js:16) at XMLHttpRequest.handleError (xhr.js:83)

this is fine as I guess we haven't handled the response yet at that point of time in the video. But check your to email and I found it in my spam folder

zeeshanejaz786 commented 2 years ago

SendGrid has now made it hard to open an account. I have tried multiple times to send multiple emails to the support team which they made necessary to and I still can't open an account. they require a whole bunch of information about your business and all the other kind of information which we don't have at this point (assuming you are following the course). so now I am using an alternative to SendGrid. it's similar in most cases. its name is Sendinblue. I would suggest if you are unable to open an account on send grid use send in blue. read the documentation and if you still need help setting it up https://github.com/sendinblue/APIv3-nodejs-library follow the link up there and you are good to go.