app.module.ts
import { Module } from '@nestjs/common';
import { MongooseModule } from '@nestjs/mongoose';
import { UsersModule } from './users/users.module';
import { PostsModule } from './posts/posts.module';
@Module({
imports: [
MongooseModule.forRoot('mongodb://127.0.0.1/nestjs_tutorial'), UsersModule, PostsModule],
controllers: [],
providers: [],
})
export class AppModule {}
post.module.ts
import { Module } from '@nestjs/common';
import { MongooseModule } from '@nestjs/mongoose';
import { Post, PostSchema } from 'src/schemas/Post.schema';
import { PostsController } from './posts.controller';
import { PostsService } from './posts.service';
import { User } from 'src/schemas/User.schema';
@Module({
imports: [
MongooseModule.forFeature([
{
name: Post.name,
schema: PostSchema,
}
]),
],
controllers: [PostsController],
providers: [PostsService],
})
export class PostsModule {}
post.controller.ts
import { Controller, Post, Body, UsePipes, ValidationPipe,} from '@nestjs/common';
import { CreatePostDto } from './dtos/CreatePost.dto';
import { PostsService } from './posts.service';
@Controller('posts')
export class PostsController {
constructor(private postsService: PostsService) {}
@Post()
@UsePipes(new ValidationPipe())
createPost(@Body() createPostDto: CreatePostDto) {
return this.postsService.createPost(createPostDto);
}
}
post.service.ts
import { Injectable } from '@nestjs/common';
import { InjectModel } from '@nestjs/mongoose';
import { Model } from 'mongoose';
import { Post } from 'src/schemas/Post.schema';
import { CreatePostDto } from './dtos/CreatePost.dto';
@Injectable()
export class PostsService {
constructor(
@InjectModel(Post.name) private postModel: Model<Post>) {}
createPost( createPostDto: CreatePostDto) {
const newPost = new this.postModel(createPostDto);
return newPost.save();
}
}
users.module.ts
import { Module } from '@nestjs/common';
import { MongooseModule } from '@nestjs/mongoose';
import { User, UserSchema } from 'src/schemas/User.schema';
import { UsersService } from './users.service';
import { UsersController } from './users.controller';
import {
UserSettings,
UserSettingsSchema,
} from 'src/schemas/UserSettings.schema';
@Module({
imports: [
MongooseModule.forFeature([
{
name: User.name,
schema: UserSchema,
},
{
name: UserSettings.name,
schema: UserSettingsSchema,
},
]),
],
providers: [UsersService],
controllers: [UsersController],
})
export class UsersModule {}
users.controller.ts
import { Controller, Post, Body, UsePipes, ValidationPipe, Get, Param, HttpException, Patch, Delete } from '@nestjs/common';
import { UsersService } from './users.service';
import { CreateUserDto } from './dto/CreateUser.dto';
import mongoose from 'mongoose';
import { UpdateUserDto } from './dto/UpdateUser.dto';
@Controller('users')
export class UsersController {
constructor(private usersService: UsersService) {}
}
users.service.ts
import { Injectable } from '@nestjs/common';
import { InjectModel } from '@nestjs/mongoose';
import { Model } from 'mongoose';
import { User } from 'src/schemas/User.schema';
import { CreateUserDto } from './dto/CreateUser.dto';
import { UpdateUserDto } from './dto/UpdateUser.dto';
import { UserSettings } from 'src/schemas/UserSettings.schema';
@Injectable()
export class UsersService {
constructor(
@InjectModel(User.name) private userModel: Model<User>,
@InjectModel(UserSettings.name)
private userSettingsModel: Model<UserSettings>,
) {}
}
here is the error:
[12:55:42 pm] Found 0 errors. Watching for file changes.
[Nest] 13149 - 28/05/2024, 12:55:45 pm LOG [NestFactory] Starting Nest application...
[Nest] 13149 - 28/05/2024, 12:55:45 pm LOG [InstanceLoader] AppModule dependencies initialized
[Nest] 13149 - 28/05/2024, 12:55:45 pm LOG [InstanceLoader] MongooseModule dependencies initialized
[Nest] 13149 - 28/05/2024, 12:55:46 pm ERROR [ExceptionHandler] Nest can't resolve dependencies of the PostsService (?). Please make sure that the argument "PostModel" at index [0] is available in the PostsModule context.
Potential solutions:
- Is PostsModule a valid NestJS module?
- If "PostModel" is a provider, is it part of the current PostsModule?
- If "PostModel" is exported from a separate @Module, is that module imported within PostsModule? @Module({ imports: [ /* the Module containing "PostModel"*/ ] })
Error: Nest can't resolve dependencies of the PostsService (?). Please make sure that the argument "PostModel" at index [0] is available in the PostsModule context.
Potential solutions:
- Is PostsModule a valid NestJS module?
- If "PostModel" is a provider, is it part of the current PostsModule?
- If "PostModel" is exported from a separate @Module, is that module imported within PostsModule? @Module({ imports: [ /* the Module containing "PostModel"*/ ] })
I had no experience in nestjs. I get error when i added “PostsModule” in imports (shown below). I have tried removing mongooseModule and UsersModule from the imports, is still the same error.
@Module({
imports: [
MongooseModule.forRoot('mongodb://127.0.0.1/nestjs_tutorial'), UsersModule, PostsModule],
controllers: [],
providers: [],
})
New contributor
user25203962 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.