我正在尝试使用@ nest-modules / mailer。我创建一个模块“ example.module.ts”和服务“ example.service.ts”。我将app.module.ts中的mailer模块加载到文档中。当我将其注入ExampleService时,嵌套抛出此错误:
[ExceptionHandler] Nest can't resolve dependencies of the ExampleService (?). Please make sure that the argument at index [0] is available in the ExampleModule context.
在@ nest-modules / mailer文档中,在App.module.ts中加载mailer允许我们将其注入整个应用程序中的所有位置。 我想念什么吗?
App.module.ts
import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { HandlebarsAdapter, MailerModule } from '@nest-modules/mailer';
import { ExampleModule } from './example/example.module';
@Module({
imports: [
MailerModule.forRootAsync({
useFactory: () => ({
transport: 'smtps://user@domain.com:pass@smtp.domain.com',
defaults: {
from:'"nest-modules" <modules@nestjs.com>',
},
template: {
dir: __dirname + '/templates',
adapter: new HandlebarsAdapter(), // or new PugAdapter()
options: {
strict: true,
},
},
}),
}),
ExampleModule,
],
controllers: [AppController],
providers: [AppService],
})
export class AppModule {}
Example.module.ts
import { Injectable, Inject } from '@nestjs/common';
import { MailerService } from '@nest-modules/mailer';
@Injectable()
export class ExampleService {
constructor(private readonly _mailerService: MailerService) {}
}
这是我的package.json
{
"name": "tp-nest",
"version": "0.0.1",
"description": "",
"author": "",
"license": "MIT",
"scripts": {
"build": "tsc -p tsconfig.build.json",
"format": "prettier --write \"src/**/*.ts\"",
"start": "ts-node -r tsconfig-paths/register src/main.ts",
"start:dev": "nodemon",
"start:debug": "nodemon --config nodemon-debug.json",
"prestart:prod": "rimraf dist && npm run build && cp -r views public dist/",
"start:prod": "NODE_ENV=production node dist/src/main.js",
"start:pm2": "NODE_ENV=production pm2 start dist/src/main.js",
"lint": "tslint -p tsconfig.json -c tslint.json",
"test": "jest",
"test:watch": "jest --watch",
"test:cov": "jest --coverage",
"test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand",
"test:e2e": "jest --config ./test/jest-e2e.json"
},
"dependencies": {
"@nest-modules/mailer": "^1.0.4",
"@nestjs/common": "^6.0.0",
"@nestjs/core": "^6.0.0",
"@nestjs/mongoose": "^6.0.0",
"@nestjs/passport": "^6.0.0",
"@nestjs/platform-express": "^6.0.0",
"@nestjs/swagger": "^3.0.2",
"automapper-ts": "^1.9.0",
"bcryptjs": "^2.4.3",
"config": "^3.0.1",
"email-check": "^1.1.0",
"email-existence": "^0.1.6",
"email-verify": "^0.2.1",
"handlebars-static": "^2.0.2",
"hbs": "^4.0.3",
"jsonwebtoken": "^8.5.1",
"legit": "^1.0.5",
"lodash": "^4.17.11",
"mailchecker": "^3.2.26",
"mongoose": "^5.4.20",
"nodemailer-html-to-text": "^3.0.0",
"passport": "^0.4.0",
"passport-jwt": "^4.0.0",
"reflect-metadata": "^0.1.12",
"rimraf": "^2.6.2",
"rxjs": "^6.3.3",
"server-accepts-email": "^1.11.1",
"swagger-ui-express": "^4.0.2",
"typegoose": "^5.6.0"
},
"devDependencies": {
"@compodoc/compodoc": "^1.1.9",
"@nestjs/testing": "^6.0.0",
"@types/bcryptjs": "^2.4.2",
"@types/config": "^0.0.34",
"@types/express": "^4.16.0",
"@types/jest": "^23.3.13",
"@types/jsonwebtoken": "^8.3.2",
"@types/lodash": "^4.14.123",
"@types/mongoose": "^5.3.24",
"@types/node": "^10.12.18",
"@types/passport": "^1.0.0",
"@types/passport-jwt": "^3.0.1",
"@types/supertest": "^2.0.7",
"jest": "^23.6.0",
"nodemon": "^1.18.9",
"prettier": "^1.15.3",
"supertest": "^3.4.1",
"ts-jest": "^23.10.5",
"ts-node": "^7.0.1",
"tsconfig-paths": "^3.7.0",
"tslint": "5.12.1",
"typescript": "^3.2.4"
},
"jest": {
"moduleFileExtensions": [
"js",
"json",
"ts"
],
"rootDir": "src",
"testRegex": ".spec.ts$",
"transform": {
"^.+\\.(t|j)s$": "ts-jest"
},
"coverageDirectory": "../coverage",
"testEnvironment": "node"
}
}
答案 0 :(得分:0)
您正在使用什么版本的nest和mailer,您可以发布package.json吗? 优良作法是确保使用最新版本
答案 1 :(得分:0)
create a ExampleService module First.
import { Module } from '@nestjs/common';
import { EmailService } from './emailService';
@Module({
providers: [EmailService],
exports: [EmailService]
})
export class EmailModule {}
And finally register your module inside the AppModule.
@Module({
imports: [
MailerModule.forRootAsync({
useFactory: () => ({
transport: { host: 'smtp.mailtrap.io', port: Number(process.env.EMAIL_PORT),
auth: { user: process.env.EMAIL_USERNAME, pass: process.env.EMAIL_PASSWORD }},
defaults: {
from:'"nest-modules" <modules@nestjs.com>',
},
template: {
dir: __dirname + '/templates',
adapter: new HandlebarsAdapter(), // or new PugAdapter()
options: {
strict: true,
},
},
}),
}),
EmailModule,
],
答案 2 :(得分:-1)
您需要更新@ nestjs / core软件包,此外,该服务与模块不同,如果要创建服务,则必须将其放在提供程序中而不是导入中