ItemService error
donPuerto opened this issue ยท 11 comments
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.
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 !
Hi I got a bit different error its in the customer service.
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?