简体   繁体   English

从NodeMailer向Swagger发送适当的响应

[英]Sending appropriate Response to Swagger from NodeMailer

I am attempting to send an email via API call (Swagger) from a NodeMailer package (version 2.7.2). 我试图通过API调用(Swagger)从NodeMailer软件包(版本2.7.2)发送电子邮件。 Functionally speaking, everything is basically working okay --- that is, the email is delivered as expected. 从功能上讲,一切基本上都可以正常工作-即按预期方式发送电子邮件。

The only thing is, I don't get a response that works for the Swagger controller that calls the nodemailer package's sendEmail command. 唯一的事情是,我没有得到对调用Nodemailer包的sendEmail命令的Swagger控制器有效的响应。

Here is the code for the nodeMailer function. 这是nodeMailer函数的代码。 This works (sends the email), and outputs the following to the console : 此方法有效(发送电子邮件),并将以下内容输出到控制台

Attempting to send mail to: ["someemail@gmail.com"] 尝试将邮件发送到:[“ someemail@gmail.com”]
250 2.0.0 OK 1550718405 w10sm28574425pge.8 - gsmtp 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); }); }); } 

However, Swagger never receives the response in info.response from sendMails callback. 但是,Swagger永远不会从sendMails回调中收到info.response中的响应。 Here is the Swagger controller that is calling the sendEmail function: 这是Swagger控制器,它正在调用sendEmail函数:

 '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); }); }; 

The ".then" function is never reached from the controller, so Swagger just stalls out and never gets a response back (just stays stuck on loading): 控制器永远不会到达“ .then”功能,因此Swagger只会停滞不前,也永远不会获得响应(只是停留在加载中):

在此处输入图片说明

Please let me know what I need to do to properly return the result from NodeMailer's callback to the function calling from the Swagger controller. 请让我知道我需要做些什么,以将NodeMailer的回调结果正确返回到从Swagger控制器调用的函数。 I have tried returning the actual sendMail function as well as returning response.info, and neither triggers the code in the Swagger controller's .then() function. 我试图返回实际的sendMail函数以及返回response.info,但都没有触发Swagger控制器的.then()函数中的代码。

I was able to solve my own question here. 我可以在这里解决自己的问题。 It turns out nodemailer already returns a Promise, so returning a promise of a promise was (reasonably) not acting as I thought a promise should. 事实证明,nodemailer已经返回了Promise,因此,返回一个Promise的诺言(合理地)并不像我认为的Promise那样。 By removing the offending / returned "new Promise" code, I was able to get an appropriate response back in the controller file by returning nodeMailer's built-in Promise function. 通过删除违规/返回的“新Promise”代码,我可以通过返回nodeMailer的内置Promise函数在控制器文件中获得适当的响应。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM