Nest 无法解析 AdminsService 的依赖关系
Nest can't resolve dependencies of the AdminsService
当我在 DoctorsService 中导入另一个模块服务时收到此错误消息:
Nest can't resolve dependencies of the AdminsService (?). Please make
sure that the argument AdminsRepository at index [0] is available in
the DoctorsModule context.
我在提供商的 doctors.module 中导入了 AdminsService,但问题没有解决。
doctors.module.ts
@Module({
imports: [PassportModuleJwt],
controllers: [DoctorsController],
providers: [DoctorsService, AdminsService],
})
admins.module.ts
@Module({
imports: [PassportModuleJwt, TypeOrmModule.forFeature([AdminsRepository])],
controllers: [AdminsController],
providers: [AdminsService],
exports: [AdminsService],
})
最后这是我的 doctors.service,这里出现错误:
@Injectable()
export class DoctorsService {
constructor(private adminsService: AdminsService) {}
async create(createDoctorDto: CreateDoctorDto): Promise<DoctorPayload> {
const { user_id, name, avatar, bio } = createDoctorDto;
await this.adminsService.findOne(user_id);
const doctor = new Doctor();
doctor.user_id = user_id;
doctor.name = name;
doctor.avatar = avatar;
doctor.bio = bio;
try {
return await doctor.save();
} catch (error) {
throw new InternalServerErrorException();
}
}
}
和我的admins.service:
@Injectable()
export class AdminsService {
constructor(
@InjectRepository(AdminsRepository)
private adminsRepository: AdminsRepository,
) {}
async findOne(id: number): Promise<Admin> {
const admin = await this.adminsRepository.findById(id);
if (!admin) {
throw new NotFoundException();
}
delete admin.password;
return admin;
}
}
怎么了?
AdminsService
从 AdminsModule
导出。要在另一个模块中使用它,您需要导入它。
// doctors.module.ts
@Module({
imports: [PassportModuleJwt, AdminsModule], // Add AdminsModule here
controllers: [DoctorsController],
providers: [DoctorsService], // Remove AdminsService here
})
请注意,DoctorsModule 不应提供 AdminsService
,因为它已由 AdminsModule
提供。
当我在 DoctorsService 中导入另一个模块服务时收到此错误消息:
Nest can't resolve dependencies of the AdminsService (?). Please make sure that the argument AdminsRepository at index [0] is available in the DoctorsModule context.
我在提供商的 doctors.module 中导入了 AdminsService,但问题没有解决。
doctors.module.ts
@Module({
imports: [PassportModuleJwt],
controllers: [DoctorsController],
providers: [DoctorsService, AdminsService],
})
admins.module.ts
@Module({
imports: [PassportModuleJwt, TypeOrmModule.forFeature([AdminsRepository])],
controllers: [AdminsController],
providers: [AdminsService],
exports: [AdminsService],
})
最后这是我的 doctors.service,这里出现错误:
@Injectable()
export class DoctorsService {
constructor(private adminsService: AdminsService) {}
async create(createDoctorDto: CreateDoctorDto): Promise<DoctorPayload> {
const { user_id, name, avatar, bio } = createDoctorDto;
await this.adminsService.findOne(user_id);
const doctor = new Doctor();
doctor.user_id = user_id;
doctor.name = name;
doctor.avatar = avatar;
doctor.bio = bio;
try {
return await doctor.save();
} catch (error) {
throw new InternalServerErrorException();
}
}
}
和我的admins.service:
@Injectable()
export class AdminsService {
constructor(
@InjectRepository(AdminsRepository)
private adminsRepository: AdminsRepository,
) {}
async findOne(id: number): Promise<Admin> {
const admin = await this.adminsRepository.findById(id);
if (!admin) {
throw new NotFoundException();
}
delete admin.password;
return admin;
}
}
怎么了?
AdminsService
从 AdminsModule
导出。要在另一个模块中使用它,您需要导入它。
// doctors.module.ts
@Module({
imports: [PassportModuleJwt, AdminsModule], // Add AdminsModule here
controllers: [DoctorsController],
providers: [DoctorsService], // Remove AdminsService here
})
请注意,DoctorsModule 不应提供 AdminsService
,因为它已由 AdminsModule
提供。