首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >无法解析依赖项的NestJS

无法解析依赖项的NestJS
EN

Stack Overflow用户
提问于 2020-03-01 19:17:22
回答 1查看 2.9K关注 0票数 1

当我运行我的NestJS应用程序时,我会收到这个错误。

代码语言:javascript
运行
复制
[Nest] 19139   - 03/01/2020, 2:10:01 PM   [ExceptionHandler] Nest can't resolve dependencies of the AccountsService (AccountRepository, ?, HashPasswordService). Please make sure that the argument Object at index [1] is available in the AccountsModule context.

Potential solutions:
- If Object is a provider, is it part of the current AccountsModule?
- If Object is exported from a separate @Module, is that module imported within AccountsModule?
  @Module({
    imports: [ /* the Module containing Object */ ]
  })
 +1ms

我有点搞不懂是什么导致了这一切。据我所知,我的代码看起来是正确的。下面是我的AccountsService类的定义:

代码语言:javascript
运行
复制
import { Injectable, ConflictException, Logger, InternalServerErrorException, NotFoundException, Inject } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Account } from 'src/accounts/entities/account';
import { Repository, FindManyOptions, UpdateDateColumn } from 'typeorm';
import { CreateAccount } from 'src/accounts/dtos/create-account';
import { GetAccountsWithFilters } from 'src/accounts/dtos/get-accounts-with-filters';
import { UpdateAccountProfileInfo } from 'src/accounts/dtos/update-account-profile-info';
import { HashPasswordService } from '../hash-password/hash-password.service';
import { UpdateEmail } from 'src/accounts/dtos/update-email';
import { UpdatePhone } from 'src/accounts/dtos/update-phone';
import { AccountRepository } from 'src/accounts/repositories/account-repository';


/**
 * AccountsService encapsulates all the actions that can be performed by an account.
 */
@Injectable()
export class AccountsService {

    constructor(
        @InjectRepository(AccountRepository) private accountRepository: AccountRepository,
        private logger = new Logger("Accounts Service"),

        @Inject(HashPasswordService)
        private hashPasswordService: HashPasswordService,
    ) { }
    // more code here
}

我的舱看起来像这样。

代码语言:javascript
运行
复制
import { Module } from '@nestjs/common';
import { AccountsService } from './services/accounts/accounts.service';
import { TypeOrmModule } from '@nestjs/typeorm';
import { Account } from './entities/account';
import { AccountsController } from './controllers/accounts/accounts.controller';
import { AccountCleanerService } from './services/account-cleaner/account-cleaner.service';
import { AuthenticationService } from './services/authentication/authentication.service';
import { AuthenticationController } from './controllers/authentication/authentication.controller';
import { HashPasswordService } from './services/hash-password/hash-password.service';
import { JwtModule } from "@nestjs/jwt";
import { PassportModule } from "@nestjs/passport";
import { JwtStrategy } from './auth-strategies/jwt-strategy';
import { AccountRepository } from './repositories/account-repository';

@Module({
  imports: [
    TypeOrmModule.forFeature([
      Account,
      AccountRepository,
    ]),
    JwtModule.register({
      secret: "SOME_APP_SECRET",
      signOptions: {
        expiresIn: 3600
      }
    }),
    PassportModule.register({
      defaultStrategy: "jwt",
    }),
  ],
  controllers: [
    AccountsController, 
    AuthenticationController,
  ],
  providers: [
    AccountRepository,
    HashPasswordService,
    AccountsService, 
    AccountCleanerService, 
    AuthenticationService,  
    JwtStrategy,
  ],
  exports: [JwtStrategy, PassportModule],
})
export class AccountsModule { }

最后,下面是应用程序模块:

代码语言:javascript
运行
复制
import { Module } from '@nestjs/common';
import { AccountsModule } from './accounts/accounts.module';
import { TypeOrmModule } from "@nestjs/typeorm";
import {Account} from "./accounts/entities/account";
import { ConfigModule } from "@nestjs/config";
import account from "./../config/account";
import auth from "./../config/auth";
import database from "./../config/database";
import server from "./../config/server";
import { AccountRepository } from './accounts/repositories/account-repository';

@Module({
  imports: [
    AccountsModule,
    ConfigModule.forRoot({
      // make this module available globally
      isGlobal: true,

      // The configuration files.
      load: [
        account,
        auth,
        database,
        server
      ],
    }),
    TypeOrmModule.forRoot({
      type: "mongodb",
      url: "my connection string here",
      entities: []
    }),
  ],
  controllers: [],
  providers: [],
})
export class AppModule { }

正如您所看到的,我清楚地将这些服务提供给了模块。因此,我有点困惑,为什么Nest不能解决依赖关系。此外,除了App模块之外,现在不应该有任何其他模块,这也是上面提供的。知道NestJS为什么要抛出这个错误吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-03-01 19:31:23

Nest在解析Logger的依赖时遇到了困难,而且由于它不在providers数组中提供,所以它无法解决它。你有三个选择:

1)将private logger = new Logger('Account Service')移动到构造函数的主体

2)将private logger = new Logger('Account Service')移动到第三个位置,并将其标记为@Optional(),以便Nest在值未知时不会抛出错误。

3)将Logger添加到AccountModuleproviders数组中,然后使用this.logger.setContext()方法正确设置上下文

内置的Logger类是@Injectable(),因此可以通过DI使用它,但您必须确保提供它就像NestJS生态系统中的任何其他提供者一样。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60478845

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档