[Документация по AccountsJS NodeMail] [https://accounts -js.netlify.com / docs / email / # example-using-nodemailer]
Я хочу отправлять почту с помощью AcountsJS. Я получаю следующую ошибку, несмотря на мою строгую реализацию того, что написано в документации. Кто-нибудь сталкивался с такой же проблемой? Интересно, что-то упускаю.
index.ts
import 'reflect-metadata';
import { MongoClient } from 'mongodb';
import { AccountsServer } from '@accounts/server';
import { AccountsPassword } from '@accounts/password';
import AccountsMongoDB from '@accounts/mongo';
import { ApolloServer, attachDirectiveResolvers } from 'apollo-server';
import { AppModule } from '@modules/app/app.module';
import * as dotenv from "dotenv";
dotenv.config();
import nodemailer from 'nodemailer';
// mongo
const PORT = process.env['PORT'];
const MONGO_URI = process.env['MONGO_URI'];
const TOKEN_SECRET = process.env['TOKEN_SECRET'];
// mail
const MAIL_HOST = process.env['MAIL_HOST'];
const MAIL_PORT = process.env['MAIL_PORT'];
const MAIL_USER = process.env['MAIL_USER'];
const MAIL_PASSWORD = process.env['MAIL_PASSWORD']
async function main() {
console.time('Bootstrap');
const mongoClient = await MongoClient.connect(MONGO_URI, { useNewUrlParser: true, useUnifiedTopology: true });
const db = mongoClient.db();
// Initiate nodemailer
const transporter = await nodemailer.createTransport(
{
host: 'smtp.mailgun.org',
port: 587,
secure: false, // true for 465, false for other ports
auth: {
user: MAIL_USER,
pass: MAIL_PASSWORD
}
}
);
// Options for AccountsServer
const options = {
sendMail: async ({ from, subject, to, text, html }) => {
await transporter.sendMail(
{
from: '"KITAPHUB ?" <noreply@kitaphub.com>', // sender address
to: 'umutyerebakmaz@gmail.com, umutyerebakmaz@hotmail.com', // list of receivers
subject: 'Hello ✔', // Subject line
text: 'Hello world?', // plain text body
html: '<b>Hello world?</b>' // html body
}
);
},
};
// AccountsServer
const accountsServer = new AccountsServer(
{
db: new AccountsMongoDB(db),
tokenSecret: TOKEN_SECRET,
sendMail: options.sendMail
},
{
password: new AccountsPassword({
// This option is called when a new user create an account
// Inside we can apply our logic to validate the user fields
validateNewUser: user => {
if (user.profile.fullname.length < 2) {
throw new Error('Ad Soyad çok kısa');
}
return user;
},
}),
}
);
const { schema } = AppModule.forRoot({
accountsServer,
db
});
attachDirectiveResolvers(schema, {
id: (_, { _id }) => _id.toString(),
});
const apolloServer = new ApolloServer({
schema,
playground: true,
introspection: true,
context: session => session,
});
const { url } = await apolloServer.listen(PORT);
console.info(`Server listening: ${url}`);
console.timeEnd('Bootstrap');
}
main().catch(console.error);
ошибка
TypeError: Не удается прочитать свойство 'createTransport' из неопределенного в main (/Users/umut/Sites/app/server/src/index.ts:28:42) в process._tickCallback (internal / process / next_tick.js: 68: 7)