JavaScript 永远不会调用回调函数
JavaScript Callback function is never called
我正在尝试使用 nodemailer 在我的 SendMail 上调用回调函数。虽然函数执行成功并发送了邮件,但是在控制台记录“邮件已发送”的回调函数并没有执行。知道我遗漏了什么吗?
var email = 'receiver@gmail.com'
async.waterfall([
function(done) {
crypto.randomBytes(20, function(err, buf) {
var token = buf.toString('hex');
return done(err, token);
});
},
function(token, done){
//some code
done(null,token);
},
function(token, done){
if(token){
var mailOptions = {
to: email,
from: 'mymail@gmail.com',
subject: 'subject',
text: 'text'
};
mailer.SendMail(mailOptions, function(err) {
if(err){
console.log(err);
}else {
console.log("no error");
}
console.log("sent mail");
req.flash('info', 'An e-mail has been sent to ' + email + ' with further instructions.');
console.log("mail sent");
done(err, 'done');
});
}
else return done("Account existiert nicht", null);
}
],
function(err){
console.log("final block");
if(err){
console.log(err);
return res.status(500).send({msg:"error"});
}
else {
return res.status(200).send({msg: "Reset Link has been sent to you."})
}
}
);
这是对应的SendMail()
函数:
const nodemailer = require("nodemailer");
exports.SendMail = (msg) => {
let transporter = nodemailer.createTransport({
service: 'gmail',
host: "smtp.gmail.com",
secure: true,
auth: {
user: "mymail@gmail.com", // my mail
pass: "mysecretpwd" // my password
}
});
return transporter.sendMail(msg, function (error, info) {
if(error) {
return console.log(error);
}
console.log("Message sent "+ info.response) // this statement is showing on the console
});
}
您的函数没有使用第二个参数(回调)。
您可以使用类似的东西:
const nodemailer = require("nodemailer");
exports.SendMail = (msg, callback) => {
let transporter = nodemailer.createTransport({
service: 'gmail',
host: "smtp.gmail.com",
secure: true,
auth: {
user: "mymail@gmail.com", // my mail
pass: "mysecretpwd" // my password
}
});
return transporter.sendMail(msg, callback);
}
我正在尝试使用 nodemailer 在我的 SendMail 上调用回调函数。虽然函数执行成功并发送了邮件,但是在控制台记录“邮件已发送”的回调函数并没有执行。知道我遗漏了什么吗?
var email = 'receiver@gmail.com'
async.waterfall([
function(done) {
crypto.randomBytes(20, function(err, buf) {
var token = buf.toString('hex');
return done(err, token);
});
},
function(token, done){
//some code
done(null,token);
},
function(token, done){
if(token){
var mailOptions = {
to: email,
from: 'mymail@gmail.com',
subject: 'subject',
text: 'text'
};
mailer.SendMail(mailOptions, function(err) {
if(err){
console.log(err);
}else {
console.log("no error");
}
console.log("sent mail");
req.flash('info', 'An e-mail has been sent to ' + email + ' with further instructions.');
console.log("mail sent");
done(err, 'done');
});
}
else return done("Account existiert nicht", null);
}
],
function(err){
console.log("final block");
if(err){
console.log(err);
return res.status(500).send({msg:"error"});
}
else {
return res.status(200).send({msg: "Reset Link has been sent to you."})
}
}
);
这是对应的SendMail()
函数:
const nodemailer = require("nodemailer");
exports.SendMail = (msg) => {
let transporter = nodemailer.createTransport({
service: 'gmail',
host: "smtp.gmail.com",
secure: true,
auth: {
user: "mymail@gmail.com", // my mail
pass: "mysecretpwd" // my password
}
});
return transporter.sendMail(msg, function (error, info) {
if(error) {
return console.log(error);
}
console.log("Message sent "+ info.response) // this statement is showing on the console
});
}
您的函数没有使用第二个参数(回调)。 您可以使用类似的东西:
const nodemailer = require("nodemailer");
exports.SendMail = (msg, callback) => {
let transporter = nodemailer.createTransport({
service: 'gmail',
host: "smtp.gmail.com",
secure: true,
auth: {
user: "mymail@gmail.com", // my mail
pass: "mysecretpwd" // my password
}
});
return transporter.sendMail(msg, callback);
}