最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Unhandled promise rejection using sendgrid and nodejs - Stack Overflow

programmeradmin0浏览0评论

I am new to these languages, I'm working on a project that provides a management of a database of users and a function for each of them, by button, to send them emails with their code. I'm using nodejs, expressjs (database) and sendgrid, these are their versions (I think they are the last ones):

"@sendgrid/mail": "^6.2.1", "sendgrid-rest": "^2.4.0", "express": "^4.16.2", node v8.9.4

I am using windows 10 and through the official sengrid guides I have obtained the following code:

exports.sendMail = function(req,res){

var name = req.params.nome;
var email = req.params.email;
var code = req.params.code;

const  sgMail = require('@sendgrid/mail');
sgMail.setApiKey('api_key');
const  msg = {
  to: email,
  from: '[email protected]',
  subject: 'event',
  text: 'Dear '+name+',\n'+'this is your code: \n',
  html: '<br><strong>'+code+'</strong><br>',
};
sgMail.send(msg).then(() => {
    res.redirect('/users');
}).catch((error) => {
    console.log('error', error);
});

}

I tried creating an environment variable via windows settings, but it did not work. I found another solution that proposed to create a .env file and to export a variable containing the api_key of sendgrid:

export SENDGRID_API_KEY = 'SG.xxx...'

but for the time being it does not seem to work, rather I receive these messages, and no e-mails are sent. I checked the correctness of the api_key and it's the one that starts with SG.XXXXX... , so I think it's right. Here are the messages:

(node:9688) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): Error: Provide at least one of to, cc or bcc
(node:9688) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

They are simple warnings, but I fear they affect the operation.

Another question: I read in some posts, that the use of api_key in a file in the project directory (I think they spoke of the public directory (/ project / public)), I put the file in the main directory of my project (/ project), has security problems because it could be seen externally, is it safe what I did? Do I have to move it to other directories?

Every help is wele, I thought it would be much easier to use the sendgrid service in a web app.If there was a faster method through sendgrid it is wele.

EDIT: Thanks to the answers I managed to arrive in part to a solution, modifying the code as above, I do not receive any error but it seems not to send any email to the mailbox. Some help?

I am new to these languages, I'm working on a project that provides a management of a database of users and a function for each of them, by button, to send them emails with their code. I'm using nodejs, expressjs (database) and sendgrid, these are their versions (I think they are the last ones):

"@sendgrid/mail": "^6.2.1", "sendgrid-rest": "^2.4.0", "express": "^4.16.2", node v8.9.4

I am using windows 10 and through the official sengrid guides I have obtained the following code:

exports.sendMail = function(req,res){

var name = req.params.nome;
var email = req.params.email;
var code = req.params.code;

const  sgMail = require('@sendgrid/mail');
sgMail.setApiKey('api_key');
const  msg = {
  to: email,
  from: '[email protected]',
  subject: 'event',
  text: 'Dear '+name+',\n'+'this is your code: \n',
  html: '<br><strong>'+code+'</strong><br>',
};
sgMail.send(msg).then(() => {
    res.redirect('/users');
}).catch((error) => {
    console.log('error', error);
});

}

I tried creating an environment variable via windows settings, but it did not work. I found another solution that proposed to create a .env file and to export a variable containing the api_key of sendgrid:

export SENDGRID_API_KEY = 'SG.xxx...'

but for the time being it does not seem to work, rather I receive these messages, and no e-mails are sent. I checked the correctness of the api_key and it's the one that starts with SG.XXXXX... , so I think it's right. Here are the messages:

(node:9688) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): Error: Provide at least one of to, cc or bcc
(node:9688) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

They are simple warnings, but I fear they affect the operation.

Another question: I read in some posts, that the use of api_key in a file in the project directory (I think they spoke of the public directory (/ project / public)), I put the file in the main directory of my project (/ project), has security problems because it could be seen externally, is it safe what I did? Do I have to move it to other directories?

Every help is wele, I thought it would be much easier to use the sendgrid service in a web app.If there was a faster method through sendgrid it is wele.

EDIT: Thanks to the answers I managed to arrive in part to a solution, modifying the code as above, I do not receive any error but it seems not to send any email to the mailbox. Some help?

Share Improve this question edited Mar 9, 2018 at 12:59 mixx asked Mar 9, 2018 at 12:01 mixxmixx 1241 gold badge2 silver badges14 bronze badges 2
  • I think the send function returns a promise so just add one .catch statement and the problem will be solved. And this will let you know why send() is failing – Saikat Hajra Commented Mar 9, 2018 at 12:03
  • Thank you for your answer, I had read something about it in some forums, but why not write it in the official documentation? As they are warning, the code should do the same, but apparently it is not. – mixx Commented Mar 9, 2018 at 12:08
Add a ment  | 

1 Answer 1

Reset to default 7

try this, You are not handling the error condition. Error: Provide at least one of to, cc or bcc may be to email is null/undefined.

sgMail.send(msg).then(() => {
    res.redirect('/users');
}).catch((error) => {
    console.log('error', error);
});
发布评论

评论列表(0)

  1. 暂无评论