从NodeMailer发送对Swagger的适当响应

问题描述 投票:0回答:1

我试图通过API调用(Swagger)从NodeMailer包(版本2.7.2)发送电子邮件。从功能上讲,一切都基本正常 - 也就是说,电子邮件按预期交付。

唯一的问题是,我没有得到适用于调用nodemailer包的sendEmail命令的Swagger控制器的响应。

这是nodeMailer函数的代码。这有效(发送电子邮件),并将以下内容输出到控制台:

试图发送邮件至:[“[email protected]”] 250 2.0.0 OK 1550718405 w10sm28574425pge.8 - gsmtp

'use strict';
const fs = require('fs');
var nodemailer = require('nodemailer');
var emailConfig = require('../configs/email.json');

/**
 * @since AlphaRC7
 * @desc Config is loaded for nodemailer via emailConfig.json,
 * for more information: see https://nodemailer.com/smtp/
 * @param emails is a comma separated string sent from the controller processing things before hand
 * 
 * @since AlphaRC8
 * @param shareUrl is a string GUID
 */

exports.sendEmail = function (shareUrl, emails, pdfContent) {
    return new Promise(function (req, resolve) {
        var transporter = nodemailer.createTransport(emailConfig);
        console.log(pdfContent.buffer);
        // setup e-mail data with unicode symbols
        var mailOptions = {
            from: emailConfig.fromSenderEmail, // sender email address
            to: emails, // list of receivers
            subject: 'Your colleague shared a report with you!',
            text: 'Hey there! Your colleague wants to collaborate with you! <br />' +
                    'Check here to visit: ' + shareUrl, // plaintext body'
            html: 'Hey there! Your colleague wants to collaborate with you! <p>' +
                    '<b>Click here to visit: </b> <a href=' + shareUrl + '>' + shareUrl + '</a></p>',
            attachments:[{
                filename: 'report.pdf',
                content:  new Buffer(pdfContent.buffer, 'binary')
            }]
        };

        console.log("Attempting to send mail to:");
        console.log(emails);
        return transporter.sendMail(mailOptions).then(function(info) {
            console.log(info.response);
        }).catch(function(err) {
            console.log(err);
        });
    });
}

但是,Swagger从未在sendMails回调的info.response中收到响应。这是调用sendEmail函数的Swagger控制器:

'use strict';

var utils = require('../utils/writer.js');
var email = require('../impl/EmailService.js');
var fs = require('fs');

/**
 * This function simply instantiates the entry, so we don't need to pass
 * it anything, just have an agreement on the security side.
 */
module.exports.sendEmail = function sendEmail (req, res, next) {
    
    var shareUrl = req.swagger.params.shareUrl.value;
    var emails = req.swagger.params.emails.value;
    var pdfBlob = req.swagger.params.myblob.value;


    email.sendEmail(shareUrl, emails, pdfBlob)
        .then(function (response) {
            console.log(response);
            res.send(response);
            utils.writeJson(res, response);
        })
        .catch(function (response) {
            console.log(response);
            res.send(response);
            utils.writeJson(res, response);
        });
};

从来没有从控制器到达“.then”函数,因此Swagger只是停止运行并且永远不会得到响应(只是在加载时停留):

enter image description here

请让我知道我需要做什么来正确地将结果从NodeMailer的回调返回到从Swagger控制器调用的函数。我已经尝试返回实际的sendMail函数以及返回response.info,并且都没有触发Swagger控制器的.then()函数中的代码。

javascript node.js callback swagger nodemailer
1个回答
0
投票

我能在这里解决自己的问题。事实证明,nodemailer已经返回了一个Promise,所以返回一个promise的承诺(合理地)没有按照我认为的承诺应该行事。通过删除违规/返回的“新Promise”代码,我可以通过返回nodeMailer的内置Promise函数在控制器文件中获得适当的响应。

© www.soinside.com 2019 - 2024. All rights reserved.