我正在尝试为nodejs中的用户构建电子邮件:
function makeOptions(user, updatedresults) {
var mailOptions = {
'from': ['contact@mywebsite.com', 'Site Name'],
'to': {
'mymail@mail.com': 'Admin'
},
'subject': "New results for your bookmark " + bookmark,
'html': '<p>Bonjour <b> ' + user.nomprenom + '!</b><br>' +
'<b>New results :<b>Updated results ' + updatedresults.length + ' :</b><ul>' + rapportENEWS(updatedresults, 'en') + '</ul>'
};
console.log(mailOptions.html);
}
function rapportENEWS(data, lang) {
var htmlbookmark = '';
var sqlbookmarks = "select id, title, pertinence where id in (" + data + ") ORDER BY pertinence";
connection.query(sqlbookmarks, function(err, res) {
var counter = 0;
for (var i = 0; i < res.length; i++) {
if (i == 0) {
htmlbookmark += '<h2 style="font-size:14px; text-decoration:underline; color:#0A4369">' + res[i].pertinence + '</h2>';
} else if (i > 0 && res[i - 1].pertinence != res[i].pertinence) {
htmlbookmark += '<h2 style="font-size:14px; text-decoration:underline; color:#0A4369">' + res[i].pertinence + '</h2>';
htmlbookmark += '<table style="width:100%;font-size:12px;">Updated articles';
htmlbookmark += "\n" + '</tr><tr><td style="padding-bottom:8px;border-bottom:3px solid #bfbfbf;" colspan="2"><strong><span style="color: #993300;">' + res[i].title + ' ' + getMagic(res[i].id) + '</span></strong></td></tr></table>';
}
if (counter == res.length) {
//HERE THE RETURN
console.log(htmlbookmark);
return htmlbookmark;
} else {
counter++;
}
}
});
}
当我查看我的控制台时,我得到undefined
console.log
mailOptions.html
)。
我在被调用的函数中创建了一些console.log
,它显示了结果,但它没有返回它。
注意:函数getMagic(res[i].id)
返回一个数字并正常工作。