0

I am basically follow this tutorial with very minor modifications:link

I am using nest 6.14.2 on node 12.10.0. When running my app I get:

> recon-backend@0.0.1 start C:\Users\e5553079\Desktop\Node_Projects\recon-backend
> nest start

[Nest] 8324   - 02/10/2020, 9:28:21 AM   [NestFactory] Starting Nest application...
[Nest] 8324   - 02/10/2020, 9:28:21 AM   [InstanceLoader] MongooseModule dependencies initialized +53ms
[Nest] 8324   - 02/10/2020, 9:28:21 AM   [ExceptionHandler] Nest can't resolve dependencies of the ReconQueryService (?). Please make sure that the argument ReconQueryModel at index [0] is available in the AppModule context.

Potential solutions:
- If ReconQueryModel is a provider, is it part of the current AppModule?
- If ReconQueryModel is exported from a separate @Module, is that module imported within AppModule?
  @Module({
    imports: [ /* the Module containing ReconQueryModel */ ]
  })
 +2ms
Error: Nest can't resolve dependencies of the ReconQueryService (?). Please make sure that the argument ReconQueryModel at index [0] is available in the AppModule context.

My files look like this:

1. app.module.ts

import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { MongooseModule } from '@nestjs/mongoose';
import { ReconQueryModule } from './reconQuery/reconQuery.module';
import { ReconQueryService } from './reconQuery/reconQuery.service';
import { ReconQueryController } from './reconQuery/reconQuery.controller';
@Module({
  imports: [
    MongooseModule.forRoot('mongodb://localhost/backend-app', { useNewUrlParser: true }),
    ReconQueryModule
  ],
  controllers: [AppController, ReconQueryController],
  providers: [AppService, ReconQueryService],
})
export class AppModule {}

2. reconQuery.service.ts

import { Injectable } from '@nestjs/common';
import { Model } from 'mongoose';
import { InjectModel } from '@nestjs/mongoose';
import { ReconQuery } from './interfaces/reconQuery.interface';
import { CreateReconQueryDTO } from './dto/create-reconQuery.dto';

@Injectable()
export class ReconQueryService {
    constructor(@InjectModel('ReconQuery') private readonly reconQueryModel: Model<ReconQuery>) { }
    // Fetch all queries
    async getAllQueries(): Promise<ReconQuery[]> {
        const queries = await this.reconQueryModel.find().exec();
        return queries;
    }
...

3. reconQuery.interface.ts

import { Document } from 'mongoose';

export interface ReconQuery extends Document {
    readonly id: number;
    readonly name: string;
    readonly sql: string;
}

4. reconQuery.module.ts

import { Module } from '@nestjs/common';
import { ReconQueryController } from './reconQuery.controller';
import { ReconQueryService } from './reconQuery.service';
import { MongooseModule } from '@nestjs/mongoose';
import { ReconQuerySchema } from './schemas/reconQuery.schema';
@Module({
  imports: [
    MongooseModule.forFeature([{ name: 'ReconQuery', schema: ReconQuerySchema }])
  ],
  controllers: [ReconQueryController],
  providers: [ReconQueryService]
})
export class ReconQueryModule {}

I wonder why it cries about ReconQueryModel as I do not have such an entity, just a ReconQuery interface.

Russ Cam
  • 124,184
  • 33
  • 204
  • 266

1 Answers1

3

You have extra ReconQueryService provided in AppModule.

import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { MongooseModule } from '@nestjs/mongoose';
import { ReconQueryModule } from './reconQuery/reconQuery.module';
import { ReconQueryService } from './reconQuery/reconQuery.service';
import { ReconQueryController } from './reconQuery/reconQuery.controller';
@Module({
  imports: [
    MongooseModule.forRoot('mongodb://localhost/backend-app', { useNewUrlParser: true }),
    ReconQueryModule
  ],
  controllers: [AppController, ReconQueryController],
  providers: [AppService, ReconQueryService], // <-- this causes the error
})
export class AppModule {}

@InjectModel() is provided by calling MongooseModule.forFeature(...) on a certain @Module(). ReconQueryService in ReconQueryModule is fine because that's where you have MongooseModule.forFeature() called so @InjectModel() is provided properly. However, in AppModule, you don't have MongooseModule.forFeature() so Nest cannot resolve the @InjectModel() for ReconQueryService.

Check if you really need to use ReconQueryService in AppModule (services and controllers declared in AppModule). If you don't, then remove ReconQueryService from providers array. If you do, then go to ReconQueryModule and do the following:

import { Module } from '@nestjs/common';
import { ReconQueryController } from './reconQuery.controller';
import { ReconQueryService } from './reconQuery.service';
import { MongooseModule } from '@nestjs/mongoose';
import { ReconQuerySchema } from './schemas/reconQuery.schema';
@Module({
  imports: [
    MongooseModule.forFeature([{ name: 'ReconQuery', schema: ReconQuerySchema }])
  ],
  controllers: [ReconQueryController],
  providers: [ReconQueryService],
  exports: [ReconQueryService] // <-- add this line
})
export class ReconQueryModule {}

exported things will be available to use outside of the module they're provided in. Just import that module somewhere else.

Chau Tran
  • 4,668
  • 1
  • 21
  • 39