模块导出功能

时间:2017-05-22 09:02:58

标签: node.js function export

我正在尝试导出使用Sendgrid发送电子邮件的函数。我创建了一个外部文件,我在其中创建了这个函数。我放入其他文件的数据(用作参数的凭据),我也导入了它。

我没有错误,但电子邮件根本就没有发送。

SEND_MAIL:

    module.exports = {

        'sendMail': function(subject, sender_email, email, link, username, template){    
         var request = sg.emptyRequest();
            request.body = {
                subject: subject,
                from: {
                email: sender_email
                },
                personalizations: [
                    {
                      to: [{                     
                         email:  email
                         }],
                      substitutions: {  
                         '[link]'    :  link, 
                         '[email]'   :  email,
                         '[username]':  username
                      }                   
                    }
                ],
                template_id: template 
            };

        request.method = "POST"; 
        request.path = "/v3/mail/send";    
         return sg.API(request).then(function (response) {
             console.log(response);                   
          }).catch(function(err){console.log(err.response.body.errors);});
   }
};

主要文件(我导入该函数):

    var config = require('../config/config.json');
    var utils= require('../service/utils');
    var api_key  = config.sendgrid.key;
    var sg = require('sendgrid')(api_key);

    .....


     function( new_user ){

                var template    = utils.new_account_template;
                var sender_email= utils.new_account_email;
                var link        = utils.new_account_link;
                var subject     = utils.new_account_subject;
                var email       = new_user.email;
                var username    = new_user.username;

                var utilemail = require('../service/utilemail')(subject, sender_email, email, link, username, template);
                utilemail.sendMail(subject, sender_email, email, link, username, template);

return res.json( rs.success({ jwt: new_user.getJwt() }) );

    }).catch(function( err ){
        return res.json( rs.errorCode(err.message) );
    });

1 个答案:

答案 0 :(得分:2)

试试这个:

var sendMail= require('../service/utilemail').sendMail;
sendMail(subject, sender_email, email, link, username, template)
.then(function(){
    return res.json( rs.success({ jwt: new_user.getJwt() }) );
}, function(error){
    return res.json( rs.errorCode(err.message) );
})