简体   繁体   中英

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). 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.

Here is the code for the nodeMailer function. This works (sends the email), and outputs the following to the console :

Attempting to send mail to: ["someemail@gmail.com"]
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); }); }); } 

However, Swagger never receives the response in info.response from sendMails callback. Here is the Swagger controller that is calling the sendEmail function:

 '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):

在此处输入图片说明

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. 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.

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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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