title: How to throw exception using nest js if user exist on mongo and else create the user?
body: <p>Here is the addUser method inside users.service.ts - </p>
  
  <pre><code>// post a single user
  async addUser(createUserDTO: CreateUserDTO): Promise&lt;User&gt; {
      const newUser = await this.userModel(createUserDTO);
      return newUser.save();
  }
  </code></pre>
  
  <p>Here is the POST controller method inside users.controller.ts - </p>
  
  <pre><code>// 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
      })
  }
  </code></pre>
  
  <p>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 - </p>
  
  <pre><code>const user = await this.userModel.findOne({ userName: createUserDTO.userName });
  </code></pre>
  
  <p>I would appreciate the help :)</p>
  
  <p>N.B: Totally newbie in <a href="https://nestjs.com/" rel="nofollow noreferrer">nest js</a></p>