Nest无法解析AuthService(?)的依赖项。请确保在AuthModule上下文中索引[0]处的参数可用

时间:2019-01-25 17:25:33

标签: nestjs

  

嵌套无法解析AuthService(?)的依赖项。请确保在AuthModule上下文中索引[0]处的参数可用。 +134毫秒   错误:Nest无法解析AuthService(?)的依赖项。请确保在AuthModule上下文中索引[0]处的参数可用。

我从https://docs.nestjs.com/techniques/authentication复制了代码,然后报告此错误:

import { Injectable, Inject } from '@nestjs/common';
import { UsersService } from '../users/users.service';
@Injectable()
export class AuthService {
  //constructor(private readonly usersService: UsersService) {}
  constructor(@Inject('UsersService') private UsersService: UsersService) {}
  async validateUser(token: string): Promise<any> {
    // Validate if token passed along with HTTP request
    // is associated with any registered account in the database
    return await this.UsersService.findOneByToken(token);
  }
}
import { Strategy } from 'passport-http-bearer';
import { PassportStrategy } from '@nestjs/passport';
import { Injectable, UnauthorizedException } from '@nestjs/common';
import { AuthService } from './auth.service';

@Injectable()
export class HttpStrategy extends PassportStrategy(Strategy) {
  constructor(private readonly authService: AuthService) {
    super();
  }

  async validate(token: string) {
    const user = await this.authService.validateUser(token);
    if (!user) {
      throw new UnauthorizedException();
    }
    return user;
  }
}
import { Module } from '@nestjs/common';
import { AuthService } from './auth.service';
import { HttpStrategy } from './http.strategy';
import { UsersModule } from '../users/users.module';
import { PassportModule } from '@nestjs/passport';
@Module({
  imports: [
    PassportModule.register({ defaultStrategy: 'bearer' }),
    UsersModule,
  ],
  providers: [AuthService, HttpStrategy],
})
export class AuthModule {}

import { Injectable, Inject } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { Users } from './users.entity';

@Injectable()
export class UsersService {
  constructor(
    @InjectRepository(Users)
    private readonly UsersRepository: Repository<Users>,
  ) {}

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

  async findOneByToken(token): Promise<Users[]> {
    return await this.UsersRepository.find();
  }
}
import { Controller, UseGuards, Get } from '@nestjs/common';
import { UsersService } from './users.service';
import { AuthGuard } from '@nestjs/passport';

@Controller('users')
export class UsersController {
  constructor(private readonly UsersService: UsersService) {}
  @Get('users')
  @UseGuards(AuthGuard())
  get() {
    return this.UsersService.findAll();
  }
}

import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { UsersService } from './users.service';
import { UsersController } from './users.controller';
import { Users } from './users.entity';
@Module({
  imports: [TypeOrmModule.forFeature([Users])],
  providers: [UsersService],
  controllers: [UsersController],
})
export class UsersModule {}

1 个答案:

答案 0 :(得分:2)

请在您的问题中添加代码UsersModule

通常,您需要从UsersService导出UsersModule