简体   繁体   English

Nest无法解析VendorsService(?)的依赖关系。 请验证[0]参数在当前上下文中是否可用

[英]Nest can't resolve dependencies of the VendorsService (?). Please verify whether [0] argument is available in thecurrent context

I am new to nest js and typescript also. 我也是嵌套js和typescript的新手。 Thanks in advance. 提前致谢。

I am getting this error continuously. 我不断收到此错误。

Nest can't resolve dependencies of the VendorsService (?). Nest无法解析VendorsService(?)的依赖关系。 Please verify whether [0] argument is available in thecurrent context. 请验证[0]参数在当前上下文中是否可用。

Here is the code 这是代码

App module 应用模块

@Module({
  imports: [ UsersModule, VendorsModule],
})
export class ApplicationModule {}

controller 控制者

@Controller()
export class VendorsController {
    constructor(private readonly vendorService: VendorsService){}
@Post()
async create(@Body() createVendorDTO: CreateVendorDTO){
    this.vendorService.create(createVendorDTO);
}

@Get()
async findAll(): Promise<Vendor[]>{
    return this.vendorService.findAll();
}
}

Service 服务

@Injectable()
export class VendorsService {

    constructor(@Inject('VendorModelToken') private readonly vendorModel: Model<Vendor>) {}

    async create(createVendorDTO: CreateVendorDTO): Promise<Vendor>{
        const createdVendor = new this.vendorModel(createVendorDTO);
        return await createdVendor.save();
    }
    async findAll(): Promise<Vendor[]>{
        return await this.vendorModel.find().exec();
    }
}

provider 提供者

export const usersProviders = [
  {
    provide: 'VendorModelToken',
    useFactory: (connection: Connection) => connection.model('Vendor', VendorSchema),
    inject: ['DbConnectionToken'],
  },
];

Module 模组

@Module({
  providers: [VendorsService],
  controllers: [VendorsController],
})
export class VendorsModule {}

VendorsModule sould declare your provider (usersProviders) in its providers , otherwise Nestjs will never be able to inject it into your service. VendorsModule会在其providers声明您的提供者(usersProviders),否则Nestjs将永远无法将其注入到您的服务中。

Unless you wanted to declare it with UsersModule (I guess you did); 除非您想使用UsersModule声明它(我想您已经声明过); in that case, UsersModule also needs it in its exports so it's made visible to other modules importing UsersModule. 在这种情况下,UsersModule在其exports也需要它,因此其他导入UsersModule的模块可以看到它。

It's either VendorsModule : usersProviders in providers , 这是不是VendorsModule :usersProviders的providers

Or UsersModule : usersProviders in both providers and exports UsersModuleprovidersexports usersProviders

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Nest 无法解析 RegisterService (?) 的依赖关系。请确保索引 [0] 处的参数在 RegisterModule 上下文中可用 - Nest can't resolve dependencies of the RegisterService (?).Please make sure that the argument at index [0] is available in the RegisterModule context Nest 无法解析 JobsService (?) 的依赖关系。 请确保索引 [0] 处的参数 JobModel 在 AppModule 上下文中可用 - Nest can't resolve dependencies of the JobsService (?). Please make sure that the argument JobModel at index [0] is available in the AppModule context Nest 无法解析...的依赖关系...请确保索引 [0] 处的参数 .. 在 - Nest can't resolve dependencies of the …Please make sure that the argument .. at index [0] is available in the Nest 无法解析 DiscoveryService (?) 的依赖项。 请确保索引 [0] 处的参数 ModulesContainer 在 - Nest can't resolve dependencies of the DiscoveryService (?). Please make sure that the argument ModulesContainer at index [0] is available in Nest无法解析依赖项 - Nest can't resolve dependencies 无法解析 ChannelService 的依赖关系(?确保索引 [0] 处的参数 ChannelRepository 在 ChannelService 上下文中可用 - Can't resolve dependencies of the ChannelService (? make sure the argument ChannelRepository at index [0] is available in the ChannelService context Nest 无法解决 - Nest can't resolve dependencies of the Nest 无法解析依赖项 - Nest can't resolve dependencies Nest 无法解析存储库的依赖项 - Nest can't resolve dependencies of repository Nest 无法解析 RestController 的依赖关系 - Nest can't resolve dependencies of the RestController
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM