我有两个文件,我的app.js和我的邮件程序模块mailer.js。
我的app.js应该在启动应用程序时发送几封电子邮件。
const express = require('express');
const app = express();
const mailer = require('./Server/mailer');
mailer.sendHtmlMail(["email1", "email2"], "Test Email", "<p>Success!</p>");
app.listen(8888, function () {
console.log('Server running on port 8888');
});
我的mailer.js执行邮件服务
const nodemailer = require('nodemailer');
const senderMail = "myEmail";
const emailTransporter = nodemailer.createTransport({
service: 'yahoo',
auth: {
user: senderMail,
pass: 'pw'
}
});
function getMailReceivers(mailReceivers){ // convert the string array to one string
var receivers = "";
for(var i = 0; i < mailReceivers.length; i++){
receivers += mailReceivers[i];
if(i < mailReceivers.length - 1)
receivers += ", ";
}
return receivers;
}
function getMailOptions(mailReceivers, subject, html){ // set the mail options and return them
return {
from: senderMail,
to: getMailReceivers(mailReceivers),
subject: subj,
html: content
};
}
module.exports = function () { // export the sendMail function here
sendHtmlMail: function(mailReceivers, subject, html){ // send the email
emailTransporter.sendMail(getMailOptions(mailReceivers, subject, html), function(error, info){
if (error) {
throw error;
} else {
console.log(info.response);
}
});
}
};
我收到此错误消息
SyntaxError: Unexpected token (
at createScript (vm.js:80:10)
at Object.runInThisContext (vm.js:139:10)
at Module._compile (module.js:599:28)
at Object.Module._extensions..js (module.js:646:10)
at Module.load (module.js:554:32)
at tryModuleLoad (module.js:497:12)
at Function.Module._load (module.js:489:3)
at Module.require (module.js:579:17)
at require (internal/module.js:11:18)
at Object.<anonymous> (C:\...\app.js:3:16)
但我不明白,是吗
at Object。 (C:... \ app.js:3:16)
说第16行的邮件对象(第3行)有错误吗?我找不到任何语法错误..
答案 0 :(得分:1)
我会从纠正
开始sendHtmlMail: function(mailReceivers, subject, html){
到
function sendHtmlMail(mailReceivers, subject, html){
或者,如果您确实需要对象文字语法,请在代码中找到允许的有效位置 - 这肯定是定义范围内的错误。
编辑:您可能希望导出返回对象的函数,即
module.exports = function () { // export the sendMail function here
return {
sendHtmlMail: function (mailReceivers, subject, html) { // send the email
emailTransporter.sendMail(getMailOptions(mailReceivers, subject, html), function (error, info) {
if (error) {
throw error;
} else {
console.log(info.response);
}
});
}
}
或只是具有函数
的对象module.exports = { // export the sendMail function here
sendHtmlMail: function (mailReceivers, subject, html) { // send the email
emailTransporter.sendMail(getMailOptions(mailReceivers, subject, html), function (error, info) {
if (error) {
throw error;
} else {
console.log(info.response);
}
});
}
}
答案 1 :(得分:1)
你应该导出这样的对象(不是函数):
module.exports = {
sendHtmlMail: function(mailReceivers, subject, html){ // send the email
emailTransporter.sendMail(getMailOptions(mailReceivers, subject, html), function(error, info){
if (error) {
throw error;
} else {
console.log(info.response);
}
});
}
}
现在,在您的app.js中,您可以导入sendHtmlMail
const { sendHtmlMail } = require('./mailer');
并像这样使用它:
sendHtmlMail(["email1", "email2"], "Test Email", "<p>Success!</p>");
或者:
const mailer = require('./mailer');
mailer.sendHtmlMail(["email1", "email2"], "Test Email", "<p>Success!</p>");
我希望你会发现这些信息很有用
答案 2 :(得分:1)
mailer.js文件的结尾是问题所在。您看到的异常来自app.js的第3行,因为那是您require()
其他文件的位置。
问题在于你将函数实例化与对象初始化混合在一起:
module.exports = function () { // export the sendMail function here
sendHtmlMail: function(mailReceivers, subject, html){ // send the email
emailTransporter.sendMail(getMailOptions(mailReceivers, subject, html), function(error, info){
if (error) {
throw error;
} else {
console.log(info.response);
}
});
}
};
您的代码期望该模块导出具有sendHtmlMail
属性的对象,因此该对象应该是对象初始值设定项:
module.exports = { // export the sendMail function here
sendHtmlMail: function(mailReceivers, subject, html){ // send the email
emailTransporter.sendMail(getMailOptions(mailReceivers, subject, html), function(error, info){
if (error) {
throw error;
} else {
console.log(info.response);
}
});
}
};