sqlite Nest无法解析AuthService(?)的依赖项,索引[0]处的UsersService在AuthModule上下文中可用

ejk8hzay  于 2023-02-23  发布在  SQLite
关注(0)|答案(1)|浏览(273)

我得到了这个错误。我试图解决这个问题几乎2天。我没有找到解决方案。谁能帮我找出这个问题。为了解决这个问题,我使用循环依赖前向引用到模块。但它没有工作。

Error: Nest can't resolve dependencies of the AuthService (?). Please make sure that the argument UsersService at index [0] is available in the AuthModule context.

Potential solutions:
- Is AuthModule a valid NestJS module?
- If UsersService is a provider, is it part of the current AuthModule?
- If UsersService is exported from a separate @Module, is that module imported within AuthModule?
  @Module({
    imports: [ /* the Module containing UsersService */ ]
  })

    at Injector.lookupComponentInParentModules (C:\Users\OSMAN MERT\Desktop\candy\node_modules\@nestjs\core\injector\injector.js:244:19)   
    at Injector.resolveComponentInstance (C:\Users\OSMAN MERT\Desktop\candy\node_modules\@nestjs\core\injector\injector.js:197:33)
    at resolveParam (C:\Users\OSMAN MERT\Desktop\candy\node_modules\@nestjs\core\injector\injector.js:117:38)
    at async Promise.all (index 0)
    at Injector.resolveConstructorParams (C:\Users\OSMAN MERT\Desktop\candy\node_modules\@nestjs\core\injector\injector.js:132:27)
    at Injector.loadInstance (C:\Users\OSMAN MERT\Desktop\candy\node_modules\@nestjs\core\injector\injector.js:58:13)
    at Injector.loadProvider (C:\Users\OSMAN MERT\Desktop\candy\node_modules\@nestjs\core\injector\injector.js:85:9)
    at C:\Users\OSMAN MERT\Desktop\candy\node_modules\@nestjs\core\injector\instance-loader.js:49:13
    at async Promise.all (index 3)
    at InstanceLoader.createInstancesOfProviders (C:\Users\OSMAN MERT\Desktop\candy\node_modules\@nestjs\core\injector\instance-loader.js:48:9)

users.service.ts

@Injectable()
export class UsersService {
  constructor(

  @InjectRepository(UserEntity) private repo: Repository<UserEntity>) {}
  
  async create(createUserDto: CreateUserDto):Promise<UserEntity> {
    const user = new UserEntity();
    user.firstname = createUserDto.firstname;
    user.lastname = createUserDto.lastname;
    user.email = createUserDto.email;
    
    return await this.repo.save(user) 

  }

  async findAll():Promise<UserEntity[]> {
    return await this.repo.find();
  }

  async findOne(id: number):Promise<UserEntity> {
    return await this.repo.findOne({
      select:['id','firstname','lastname',"email"],
      where:{id}
    });
  }

users.module.ts

@Module({
  imports:[TypeOrmModule.forFeature([UserEntity]),PassportModule,forwardRef(() =>AuthModule)],
  controllers: [UsersController],
  providers: [UsersService],
  exports:[UsersService]
})
export class UsersModule {}

users.entity.ts

@Entity()
export class UserEntity {
    @PrimaryGeneratedColumn()
    id: number;
   
    @Column()
    firstname: string;

    @Column()
    lastname: string;

    @Column()
    email:string;
    
   
}

auth.service.ts

@Injectable()
export class AuthService {
    constructor(@Inject('UsersService') private  userService:UsersService){}

    async validateUser(id:number,password:string){
        const user = await this.userService.findOne(id)
        if(user.id === id){
            return user
        }
        return null
    }
}

auth.module.ts

@Module({
  imports:[forwardRef(() => UsersModule)],
  providers: [AuthService,UsersService],
  exports:[AuthService]
})
export class AuthModule {}

这是我的文件夹结构:enter image description here

yzxexxkh

yzxexxkh1#

当你导入一个模块时,你导入的是它导出的提供程序。我看不出有任何理由把UsersService放在AuthModule,因为你已经把UsersModule导入到它里面了。
如果你使用的是nestjs v8或更高版本,那么使用字符串作为基于类的提供者的令牌是不起作用的,所以从那里删除@Inject('UsersService'),这可能是问题的原因。
改为@Inject(forwardRef(() => UserService))
另外,文件夹结构也不重要。只要确保你没有一堆可能引入循环导入的桶文件。你可以在这里了解更多:NestJS中的循环依赖及其避免

相关问题