我正在尝试将template_subject
变量用作电子邮件发送的subject
。
当我使用findOne
&提取mongodb中的数据时设置变量template_subject
。它只给了我undefined
的价值。
我已经从各方面对其进行了测试,数据完全来自后端,只是没有设置为variable
。
有人对此有一些解决方案吗?
exports.sendMailMsg = function (templateName, email) {
var nodemailer = require("nodemailer");
var template_subject;
var template_html;
Template.findOne({name: templateName}, function (err, template) {
template_subject = template.subject;
template_html = template.dataMsg;
});
//----- Email Options -----//
var mailOptions = {
from: "Xyz <foo@blurdybloop.com>", // sender address
to: email, // list of receivers
subject: template_subject, // Subject line
html: "<b>Hello,</b><br/><br/> You are successfuly Registered"
};
答案 0 :(得分:2)
这是因为findOne函数是异步的,因此在获取结果时,已经定义了mailOptions变量。 所以也许你可以这样做:
exports.sendMailMsg = function (templateName, email) {
var nodemailer = require("nodemailer");
var template_subject;
var template_html;
Template.findOne({name: templateName}, function (err, template) {
template_subject = template.subject;
template_html = template.dataMsg;
//----- Email Options -----//
var mailOptions = {
from: "Xyz <foo@blurdybloop.com>", // sender address
to: email, // list of receivers
subject: template_subject, // Subject line
html: "<b>Hello,</b><br/><br/> You are successfuly Registered"
};
//Do all the processing here...
});