bradtraversy/nestjs_rest_api

ItemService error

donPuerto opened this issue ยท 11 comments

image
Any idea?

imadu commented

Hi I got the same error on my console, at first, it was because I didn't install the @types/mongoose module, but I did that, I got another error in my console (as seen in the screenshot below)
I think the problem is from Mongoose, but I have no way of fixing it...Any solution?
image

edit items.module.ts like this:

import { Module } from '@nestjs/common';
import { MongooseModule } from '@nestjs/mongoose';
import { ItemsController } from './items.controller';
import { ItemsService } from './items.service';
import { ItemSchema } from './schemas/item.schema';

@module({
imports: [MongooseModule.forFeature([{ name: 'Item', schema: ItemSchema }])],
controllers: [ItemsController],
providers: [ItemsService],

exports: [MongooseModule.forFeature([{ name: 'Item', schema: ItemSchema }])], //add this line

})
export class ItemsModule {}

edit items.module.ts like this:

import { Module } from '@nestjs/common';
import { MongooseModule } from '@nestjs/mongoose';
import { ItemsController } from './items.controller';
import { ItemsService } from './items.service';
import { ItemSchema } from './schemas/item.schema';

@module({
imports: [MongooseModule.forFeature([{ name: 'Item', schema: ItemSchema }])],
controllers: [ItemsController],
providers: [ItemsService],

exports: [MongooseModule.forFeature([{ name: 'Item', schema: ItemSchema }])], //add this line

})
export class ItemsModule {}

This works..@tihssiefiL could you explain why this works?

The right way to fix this is remove ItemsController and ItemsService from app.module.ts as they have been respectively imported in the ItemsModule.

@module({
imports: [
ItemsModule,
MongooseModule.forRoot('mongodb://localhost:27017/TodoApp'),
],
controllers: [AppController],
providers: [AppService],
})

app.module.ts should be changed like this.

Hi I got a bit different error its in the customer service.
Screenshot (575)

This is what I have on my customer interface
export interface Customer {
id?: string;
name: string;
surname: string;
}

Then this is what is on my schema

import * as mongoose from "mongoose";

export const CustomerSchema = new mongoose.Schema({
name: String,
surname: String,
});

The right way to fix this is remove ItemsController and ItemsService from app.module.ts as they have been respectively imported in the ItemsModule.

@module({
imports: [
ItemsModule,
MongooseModule.forRoot('mongodb://localhost:27017/TodoApp'),
],
controllers: [AppController],
providers: [AppService],
})

app.module.ts should be changed like this.

This fixed my issue and seems cleaner than a previous answer that also works. I would recommend this one. Thanks, @rakeshpetit !

If removing duplicate controller and service not work then you must check that your model name in item.module.ts and item.service.ts are same.

check the image for better understanding

image

Hi I got a bit different error its in the customer service.
Screenshot (575)

This is what I have on my customer interface
export interface Customer {
id?: string;
name: string;
surname: string;
}

Then this is what is on my schema

import * as mongoose from "mongoose";

export const CustomerSchema = new mongoose.Schema({
name: String,
surname: String,
});

check that your model name in customer.module.ts and customer.service.ts are same.

@Rajesh-Royal
Extend your Customer Interface with Document like this:

import { Document } from 'mongoose'

export interface Customer extends Document{
name: string;
surname: string;
}

The right way to fix this is remove ItemsController and ItemsService from app.module.ts as they have been respectively imported in the ItemsModule.

@module({ imports: [ ItemsModule, MongooseModule.forRoot('mongodb://localhost:27017/TodoApp'), ], controllers: [AppController], providers: [AppService], })

app.module.ts should be changed like this.

+1 for this.
But the original source still has the duplicate imports. Why does it still run? Is this breaking now because of some changes to the framework?