title: Nodejs + typescript how to use class models?
body: <p>I am trying to learn how to use class models in nodejs and I will give you an example of what I am trying to achieve. </p>
  
  <p>For example I have <strong>user.ts model</strong> :</p>
  
  <pre><code>export default class User {
  
      private firstname: string;
      private lastname: string;
      private email: string;
      private password: string;
      private isVerified: boolean;
  
      getFirstName() {
          return this.firstname;
      }
  
      setFirstName(val: string) {
          this.firstname = val;
      }
  
      getLastName() {
          return this.lastname;
      }
  
      setLastName(val: string) {
          this.lastname = val;
      }
  
      getEmail() {
          return this.email;
      }
  
      setEmail(val: string) {
          this.email = val;
      }
  
      getPassword() {
          return this.password;
      }
  
      setPassword(val: string) {
          this.password = val;
      }
  }
  </code></pre>
  
  <p>And I have <strong>auth.ts</strong> route : </p>
  
  <pre><code>router.post('/login', async (req: Request, res: Response) =&gt; {
      const { error } = loginValidation(req.body);
      if (error) return res.status(400).send(error.details);
  
      const user = await UserModel.findOne({ email: req.body.email });
      if (!user) return res.status(400).send('Email doesn`t exist');
      if (!user.isVerified) return res.status(400).send('User is not verified, check Your email first');
  
      const validPass = await bcrypt.compare(req.body.password, user.password);
      if(!validPass) return res.status(400).send('Invalid password');
          const userResponse = new UserModel({
              firstname: user.firstname,
              lastname: user.lastname,
              email: user.email,
              createdAt: user.createdAt,
              updatedAt: user.updatedAt,
              isVerified: req.body.isVerified
          });
  
          try {
              const token = jwt.sign({_id: user._id}, process.env.TOKEN_SECRET,{ expiresIn: '8h' });
              res.header('auth-token', token);
              res.send(userResponse);
          } catch (err) {
              res.status(400).send(err);
          }
      }
  </code></pre>
  
  <p>I want to use my user.model for req.body, but don't know how : </p>
  
  <p><strong>Tryed :</strong> </p>
  
  <p>const user: User = req.body;</p>
  
  <p>user.getFirstname() - getFirstname() is not a function</p>
  
  <p>user.setFirstname(req.body.firstname) &lt;- setFirstname is not a function()</p>
  
  <p><strong>How can I attach req.body to user model ?</strong></p>