Here is the addUser method inside users.service.ts -
// post a single user
async addUser(createUserDTO: CreateUserDTO): Promise<User> {
const newUser = await this.userModel(createUserDTO);
return newUser.save();
}
Here is the POST controller method inside users.controller.ts -
// add a user
@Post('/create')
async addUser(@Res() res, @Body() createUserDTO: CreateUserDTO) {
const user = await this.usersService.addUser(createUserDTO);
return res.status(HttpStatus.OK).json({
message: "User has been created successfully",
user
})
}
I've tried using mongo collection findOne method and got user details if it's already present in mongo but not able understand how to throw exception when exist and when not exist add it in mongodb in nest js style -
const user = await this.userModel.findOne({ userName: createUserDTO.userName });
I would appreciate the help :)
N.B: Totally newbie in nest js