簡體   English   中英

從NodeMailer向Swagger發送適當的響應

[英]Sending appropriate Response to Swagger from NodeMailer

我試圖通過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中的響應。 這是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); }); }; 

控制器永遠不會到達“ .then”功能,因此Swagger只會停滯不前,也永遠不會獲得響應(只是停留在加載中):

在此處輸入圖片說明

請讓我知道我需要做些什么,以將NodeMailer的回調結果正確返回到從Swagger控制器調用的函數。 我試圖返回實際的sendMail函數以及返回response.info,但都沒有觸發Swagger控制器的.then()函數中的代碼。

我可以在這里解決自己的問題。 事實證明,nodemailer已經返回了Promise,因此,返回一個Promise的諾言(合理地)並不像我認為的Promise那樣。 通過刪除違規/返回的“新Promise”代碼,我可以通過返回nodeMailer的內置Promise函數在控制器文件中獲得適當的響應。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM