Nest can't resolve dependencies of the ProductService (?). Please verify whether [0] argument is available in the current context.
See original GitHub issueI am having trouble setting up an API that uses MongoDb.
I have a feature Module: ProductsModule which is imported in my AppModule together with the MongooseModule.
When I try to inject my ProductService into my ProductsController, I get the following error: Nest can’t resolve dependencies of the ProductService (?). Please verify whether [0] argument is available in the current context.
I already saw several issues with the same problem, but none of them could give a solution for me. Thanks for the help!
Here is my setup:
- AppModule
import { Module } from '@nestjs/common';
import { MongooseModule } from '@nestjs/mongoose';
import { ProductsModule } from 'products/products.module';
import { Mongoose } from 'mongoose';
@Module({
imports: [
MongooseModule.forRoot(
'mongodb://username:password@ds237620.mlab.com:37620/store',
),
ProductsModule,
],
})
export class AppModule {}
- ProductsModule
import { Module } from '@nestjs/common';
import { MongooseModule } from '@nestjs/mongoose';
import { ProductSchema } from './schemas/product.schema';
import { ProductService } from './product.service';
import { ProductsController } from './products.controller';
@Module({
imports: [
MongooseModule.forFeature([
{
name: 'Product',
schema: ProductSchema,
},
]),
],
controllers: [ProductsController],
providers: [ProductService],
})
export class ProductsModule {}
- ProductsController
import { Body, Controller, Delete, Get, Param, Post, Put } from '@nestjs/common';
import { CreateProductDto } from './dto/create-product.dto';
import { Product } from './interfaces/product.interface';
import { ProductService } from './product.service';
@Controller('products')
export class ProductsController {
constructor(private readonly productService: ProductService) {}
@Get()
async findAll(): Promise<Product[]> {
return this.productService.findAll();
}
@Post()
async create(@Body() createProductDto: CreateProductDto) {
this.productService.create(createProductDto);
}
}
- ProductService
import { Injectable } from '@nestjs/common';
import { InjectModel } from '@nestjs/mongoose';
import { Model } from 'mongoose';
import { PRODUCT_MODEL_TOKEN } from '../constants';
import { CreateProductDto } from './dto/create-product.dto';
import { Product } from './interfaces/product.interface';
@Injectable()
export class ProductService {
constructor(@InjectModel(PRODUCT_MODEL_TOKEN) private readonly productModel: Model<Product>) {}
async create(createProductDto: CreateProductDto): Promise<Product> {
const createdProduct = new this.productModel(createProductDto);
return await createdProduct.save();
}
async findAll(): Promise<Product[]> {
return await this.productModel.find().exec();
}
}
- Constants
export const PRODUCT_MODEL_TOKEN = 'ProductModelToken';
Issue Analytics
- State:
- Created 5 years ago
- Comments:6
Top Results From Across the Web
[ExceptionHandler] Nest can't resolve dependencies of the ...
Current behavior. Trying to use my fileRepository: Repository<File> in my FileService, but I get the following error:
Read more >Nest can't resolve dependencies of the VendorsService ...
Nest can't resolve dependencies of the VendorsService (?). Please verify whether [0] argument is available in thecurrent context. Here is the ...
Read more >nest can't resolve dependencies of the httpservice - You.com
Nest can't resolve dependencies of the MylibService (?). Please make sure that the argument HttpService at index [0] is available in the RootTestModule...
Read more >NestJS: Resolving Dependency Injection - Tevpro
Nest can't resolve dependencies of the AuthController (?). Please make sure that the argument dependency at index [0] is available in the AuthModule...
Read more >Common errors - FAQ - A progressive Node.js framework
"Cannot resolve dependency" error. The most common culprit of the error, is not having the <provider> in the module's providers array. Please make...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@ArneTesch That’s because in your
ProductsModule
you defined your DB provider’s name as'Product'
and not'ProductModelToken'
- ProductsModule
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.