[英]Importing function from another module in node js
Importing certain function from some folder which has needed module with this function doesn't work. 无法从需要此模块的模块的文件夹中导入某些功能。
I'm using nodemailer for sending emails. 我正在使用nodemailer发送电子邮件。 I have 3 different folders with modules. 我有3个带有模块的文件夹。 The problem is in importing (require) email sending function to current module from another one. 问题在于从另一模块导入(要求)电子邮件发送功能到当前模块。 It becomes undefined
and error is myFunc is not a function
. 它变得undefined
,错误是myFunc is not a function
。 I'm doing pretty simple things like requiring function from folder with index.js
which includes needed function. 我正在做一些非常简单的事情,例如从index.js
文件夹中要求功能,其中包括所需的功能。 But it becomes undefined when I try to use it. 但是当我尝试使用它时,它变得不确定。
services/mailTransport.js 服务/ mailTransport.js
const nodemailer = require('nodemailer');
const mailTransporter = nodemailer.createTransport({
host: 'smtp.gmail.com',
port: 587,
secure: false,
auth: {
user: 'test@test.com',
pass: 'myPassword'
}
});
module.exports = mailTransporter;
services/index.js 服务/ index.js
const mailTransporter = require("./mailTransporter");
module.exports = { mailTransporter }
utils/mailTemplate.js utils的/ mailTemplate.js
const { mailTransporter } = require("../services");
const sendEmail = function (obj, msg) {
return new Promise( (res, rej) => {
let mailOptions = {
from: 'test@test.com',
to: `${obj.to}`,
subject: `${obj.subject}`,
text: "plain text",
html: "<b>" + msg + "</b>"
};
mailTransporter.sendMail(mailOptions, (error, info) => {
mailTransporter.close();
if (error) {
rej(error);
}
console.log('Message sent: %s', info.messageId);
res(info.messageId);
});
})
}
module.exports = { sendEmail };
And finally I want to use it here in projects/emails.js 最后我想在project / emails.js中使用它
const { sendEmail } = require("../utils/mailTemplate");
const { vendorNotificationMessage } = require("../utils/emailMessages");
async function notifyVendors(steps) {
try {
for(let step of steps) {
if(step.vendor) {
const message = vendorNotificationMessage(step);
step.to = step.vendor.email;
step.subject = "Step cancelling notification!";
await sendEmail(step, message);
}
}
} catch(err) {
console.log(err);
console.log("Error in notifyVendors");
}
}
module.exports = { notifyVendors };
I expect that email will be sent using that sendEmail
function. 我希望使用该sendEmail
函数发送电子邮件。 But it stops with the error TypeError: sendEmail is not a function
. 但是它以错误TypeError: sendEmail is not a function
停止TypeError: sendEmail is not a function
。
The correct syntax for exporting something from a module is 从模块导出内容的正确语法是
exports.globalObjectName = localObjectName
So in your first file the export statement should look like this 因此,在第一个文件中,export语句应如下所示
exports.mailTransporter = mailTransporter
I do not think that you need {} when you use module.exports and require. 我认为在使用module.exports并要求时不需要{}。 Try module.exports = sendEmail ;
试试module.exports = sendEmail ;
and const sendEmail = require("../utils/mailTemplate");
和const sendEmail = require("../utils/mailTemplate");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.