question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

I 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:closed
  • Created 5 years ago
  • Comments:6

github_iconTop GitHub Comments

2reactions
AntoineMaitrecommented, May 29, 2018

@ArneTesch That’s because in your ProductsModule you defined your DB provider’s name as 'Product' and not 'ProductModelToken'

- 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', // <== This is the name of what you'll use later in your `@InjectModel()`
        schema: ProductSchema,
      },
    ]),
  ],
  controllers: [ProductsController],
  providers: [ProductService],
})
export class ProductsModule {}
0reactions
lock[bot]commented, Sep 24, 2019

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found