【问题标题】:Sending appropriate Response to Swagger from NodeMailer从 NodeMailer 向 Swagger 发送适当的响应
【发布时间】:2019-02-24 12:30:03
【问题描述】:

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

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

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

正在尝试发送邮件至:["someemail@gmail.com"]
250 2.0.0 好的 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 只会停止并且永远不会得到响应(只是停留在加载中):

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

【问题讨论】:

    标签: javascript node.js callback swagger nodemailer


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-11-01
      • 2020-03-06
      • 1970-01-01
      • 2019-06-29
      • 1970-01-01
      • 2015-05-13
      • 2011-09-21
      • 2012-04-24
      相关资源
      最近更新 更多