Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot destructure property 'templateName' of 'precompile(...)' #570

Closed
hddananjaya opened this issue Apr 28, 2021 · 7 comments
Closed

Cannot destructure property 'templateName' of 'precompile(...)' #570

hddananjaya opened this issue Apr 28, 2021 · 7 comments

Comments

@hddananjaya
Copy link

Hi all, 🦄

Config:

    "@nestjs-modules/mailer": "^1.6.0",
    "nodemailer": "^6.5.0",
    "handlebars": "^4.7.7",

I'm trying to use with handlebars.

App Module:

@Module({
  imports: [
    UserModule,
    AdminModule,
    HotelModule,
    TypeOrmModule.forRoot(),
    ConfigModule.forRoot({ isGlobal: true }),
    MailerModule.forRootAsync({
      useFactory: () => ({
        transport:
          'smtps://no-reply@XXX:XXX@XXX.com',
        defaults: {
          from: '"XXX" <no-reply@XXX.com>',
        },
        template: {
          dir: path.resolve(__dirname, 'templates'),
          adapter: new HandlebarsAdapter(),
          options: {
            strict: true,
          },
        },
      }),
    }),
  ],
  controllers: [AppController],
  providers: [AppService],
})

Service:

import { Injectable } from '@nestjs/common';
import { MailerService } from '@nestjs-modules/mailer';

@Injectable()
export class UserMailService {
  constructor(private readonly mailerService: MailerService) {}

  sendEmailConfirmation(): void {
    this.mailerService
      .sendMail({
        to: 'test@nestjs.com',
        from: 'noreply@nestjs.com',
        subject: 'Testing Nest Mailermodule with template ✔',
        template: '/welcome',
      })
      .then((success) => {
        console.log(success);
      })
      .catch((err) => {
        console.log(err);
      });
  }
}

I get this error when I use templates.

TypeError: Cannot destructure property 'templateName' of 'precompile(...)' as it is undefined.
    at HandlebarsAdapter.compile (/mnt/c/Users/user/Desktop/web/api/node_modules/@nestjs-modules/mailer/dist/adapters/handlebars.adapter.js:52:17)
    at /mnt/c/Users/user/Desktop/web/api/node_modules/@nestjs-modules/mailer/dist/mailer.service.js:48:40
    at processPlugins (/mnt/c/Users/user/Desktop/web/api/node_modules/nodemailer/lib/mailer/index.js:279:13)
    at /mnt/c/Users/user/Desktop/web/api/node_modules/nodemailer/lib/mailer/index.js:283:17
    at Mail._convertDataImages (/mnt/c/Users/user/Desktop/web/api/node_modules/nodemailer/lib/mailer/index.js:387:20)
    at Mail._defaultPlugins.compile (/mnt/c/Users/user/Desktop/web/api/node_modules/nodemailer/lib/mailer/index.js:31:41)
    at processPlugins (/mnt/c/Users/user/Desktop/web/api/node_modules/nodemailer/lib/mailer/index.js:279:13)
    at Mail._processPlugins (/mnt/c/Users/user/Desktop/web/api/node_modules/nodemailer/lib/mailer/index.js:287:9)
    at Mail.sendMail (/mnt/c/Users/user/Desktop/web/api/node_modules/nodemailer/lib/mailer/index.js:164:14)
    at MailerService.<anonymous> (/mnt/c/Users/user/Desktop/web/api/node_modules/@nestjs-modules/mailer/dist/mailer.service.js:74:51)
(node:8093) UnhandledPromiseRejectionWarning: Error: ENOENT: no such file or directory, open '/welcome.hbs'
    at Object.openSync (fs.js:476:3)
    at Object.readFileSync (fs.js:377:35)
    at precompile (/mnt/c/Users/user/Desktop/web/api/node_modules/@nestjs-modules/mailer/dist/adapters/handlebars.adapter.js:34:41)
    at HandlebarsAdapter.compile (/mnt/c/Users/user/Desktop/web/api/node_modules/@nestjs-modules/mailer/dist/adapters/handlebars.adapter.js:52:34)
    at /mnt/c/Users/user/Desktop/web/api/node_modules/@nestjs-modules/mailer/dist/mailer.service.js:48:40
    at processPlugins (/mnt/c/Users/user/Desktop/web/api/node_modules/nodemailer/lib/mailer/index.js:279:13)
    at /mnt/c/Users/user/Desktop/web/api/node_modules/nodemailer/lib/mailer/index.js:283:17
    at Mail._convertDataImages (/mnt/c/Users/user/Desktop/web/api/node_modules/nodemailer/lib/mailer/index.js:387:20)
    at Mail._defaultPlugins.compile (/mnt/c/Users/user/Desktop/web/api/node_modules/nodemailer/lib/mailer/index.js:31:41)
    at processPlugins (/mnt/c/Users/user/Desktop/web/api/node_modules/nodemailer/lib/mailer/index.js:279:13)
(Use `node --trace-warnings ...` to show where the warning was created)
(node:8093) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:8093) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

I manually copied templates folder to dist to check whether the problem is with paths.
But it asks for templateName which I didn't provide.

Thanks!

@hddananjaya
Copy link
Author

I was able to fix this by adding a ./ to template; as mentioned in #569
Please close this issue if not needed. Thanks.

@subalee
Copy link

subalee commented Sep 23, 2021

That is actually not a complete solution, at least not for me.

What you actually have to do is update nest-cli.json to actually copy the templates to the dist folder

nest-cli.json

"sourceRoot": "src",
"root": "./",
"compilerOptions": {
    "assets": [
      {
        "include": "notification/email-templates/**/*.hbs",
        "outDir": "dist/src"
      }
    ],
    "watchAssets": true
  }

where notification is the directory of my module and and my templates are inside the email-templates directory.
The path is actually copied that's why the outDir only has dist/src there.

however you still need to include the ./[template-name] when trying to send an email.

I actually figured this out thanks to this discussion

@cadmax
Copy link

cadmax commented Mar 2, 2022

tks @subalee

@emilenfc
Copy link

Thank you @subalee

@juandav juandav closed this as completed Dec 22, 2023
@joler023
Copy link

joler023 commented Feb 9, 2024

thank you @subalee

@RashJrEdmund
Copy link

thank you @subalee

  • Input
    Screenshot from 2024-07-06 08-32-15

  • OUtput in dist
    Screenshot from 2024-07-06 08-38-00

@IT-WIBRC
Copy link

IT-WIBRC commented Jul 12, 2024

@RashJrEdmund I encountered a problem with your config. It takes only the templates folder and files with the .hbs extension inside it, and not the images folder and other one at the same level as the templates.

Here is my config:
include: mail/templates/**
outDir: dist/src

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants