title: User models with angular 2
body: <p>So I have been starring at this thing for days now and I am lost on what I should try or maybe look for. My problem is every time I try to create a user from the website it says user can be created. If I use postman or something like it I can create it with no problem so I know it isn't something with the route since I can post to the route from postman. Its only telling me I cant create the user when i try to run the site locally using <code>ng serve</code> from the angular src folder. Is there possibly something that I am missing from this code sample?Also as you can see it is creating the user object but its not actually creating the user or pushing it to the mongo db both of which again I can create using postman.  Any help is appreciated<a href="https://i.stack.imgur.com/UYgeh.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/UYgeh.png" alt="enter image description here"></a></p>
  
  <h1>User Routes</h1>
  
  <pre><code>const express  = require('express');
  const router   = express.Router();
  const User     = require('../models/user');
  const config   = require('../config/database');
  const passport = require('passport');
  const jwt      = require('jsonwebtoken');
  
  // Register
  router.post('/register', (req, res, next) =&gt; {
      let newUser = new User({
          name:       req.body.name,
          username:   req.body.username,
          password:   req.body.password,
          email:      req.body.email,
          phone:      req.body.phone,
          state:      req.body.state,
          city:       req.body.city,
          experience: req.body.experience,
          specialty:  req.body.specialty,
          wages:      req.body.wages
      });
  
      User.addUser(newUser, (err, user) =&gt; {
        if(err) {
            res.json({success: false, message: 'User could not be created' + err});
        } else {
            res.json({success: true, message: 'User created successfully!'});
        }
      })
  });
  
  // Authenticate
  router.post('/auth', (req, res, next) =&gt; {
      const username = req.body.username;
      const password = req.body.password;
  
      User.getUserByUsername(username, (err, user) =&gt; {
          if(err) throw err;
          if(!user) {
              return res.json({success: false, message: 'User not found'});
          }
  
          User.comparePassword(password, user.password, (err, isMatch) =&gt;{
              if(err) throw err;
              if(isMatch){
                  const token = jwt.sign(user, config.secret,{
                      expiresIn: 604800 //1 week
                  });
                  res.json({
                      success: true,
                      token: 'JWT ' + token,
                      user: {
                          id: user._id,
                          name:       user.name,
                          username:   user.username,
                          email:      user.email,
                          phone:      user.phone,
                          state:      user.state,
                          city:       user.city,
                          experience: user.experience,
                          specialty:  user.specialty,
                          wages:      user.wages
                      }
                  })
              } else {
                  return res.json({success: false, message: 'invalid email and password combination please try again'});
              }
          })
      })
  });
  
  // Protected Profile
  router.get('/profile', passport.authenticate('jwt', {session: false}), (req, res, next) =&gt; {
      res.json({user: req.user});
  });
  
  
  module.exports = router;
  </code></pre>
  
  <h1>Register component</h1>
  
  <pre><code>import { Component, OnInit } from '@angular/core';
  import { ValidateService } from '../../services/validate.service'
  
  @Component({
    selector: 'app-register',
    templateUrl: './register.component.html',
    styleUrls: ['./register.component.css']
  })
  export class RegisterComponent implements OnInit {
  
    name:       String;
    username:   String;
    password:   String;
    email:      String;
    phone:      String;
    state:      String;
    city:       String;
    experience: String;
    specialty:  String;
    wages:      String;
  
    constructor(private validateService: ValidateService) {
    }
  
    ngOnInit() {
    }
  
    onRegisterSubmit() {
     const user = {
        name:       this.name,
        username:   this.username,
        password:   this.password,
        email:      this.email,
        phone:      this.phone,
        state:      this.state,
        city:       this.city,
        experience: this.experience,
        specialty:  this.specialty,
        wages:      this.wages
      };
     console.log(user);
  
      // Required Fields
      if(!this.validateService.validateRegister(user)){
        // this.flashMessage.show('Please fill in all fields', {cssClass: 'alert-danger', timeout: 3000});
        console.log('User could not be created');
        return false;
      }
  
      // Validate Email
      if(!this.validateService.validateEmail(user.email)){
        // this.flashMessage.show('Please use a valid email', {cssClass: 'alert-danger', timeout: 3000});
        return false;
      }
   }
  }
  </code></pre>
  
  <h1>User Model</h1>
  
  <pre><code>const mongoose = require('mongoose');
  const bcrypt   = require('bcryptjs');
  const config   = require('../config/database');
  
  
  // User Schema
  const UserSchema = mongoose.Schema({
      name: {
          type:     String,
          required: false
      },
      username: {
          type:     String,
          required: false
       },
      password: {
          type:     String,
          required: false
      },
      email: {
          type:     String,
          required: false
      },
      phone: {
          type:     String,
          required: false
      },
      state: {
          type:     String,
          required: false
      },
      city: {
          type:     String,
          required: false
      },
      experience: {
          type:     String,
          required: false
      },
      specialty: {
          type:     String,
          required: false
      },
      wages: {
          type:     String,
          required: false
      }
  });
  
  const User = module.exports = mongoose.model('User', UserSchema);
  
  module.exports.getUserById = function (id, callback) {
      User.findById(id, callback);
  };
  module.exports.getUserByUsername = function (username, callback) {
      const query = {username: username};
      User.findOne(query, callback);
  };
  
  module.exports.addUser = function (newUser, callback) {
      bcrypt.genSalt(10, (err, salt) =&gt; {
          if(err) throw err;
          bcrypt.hash(newUser.password, salt, (err, hash) =&gt; {
             newUser.password = hash;
             newUser.save(callback);
          });
      });
  };
  
  
  module.exports.comparePassword = function (candidatePassword, hash, callback) {
      bcrypt.compare(candidatePassword, hash, (err, isMatch) =&gt; {
          if(err) throw err;
          callback(null, isMatch);
      })
  };
  </code></pre>
  
  <h1>Html register page</h1>
  
  <pre><code>&lt;h1 class="page-header text-center"&gt;We are happy you want to join us!&lt;/h1&gt;
  
  &lt;form (submit)="onRegisterSubmit()"&gt;
    &lt;div class="form-group"&gt;
      &lt;label&gt; Full Name: &lt;span&gt;*&lt;/span&gt;&lt;/label&gt;
      &lt;input type="text" [(ngModel)]="name" name="name" class="form-control" placeholder="Jon Doe"&gt;
    &lt;/div&gt;
  
    &lt;div class="form-group"&gt;
      &lt;label&gt;Username:&lt;span&gt;*&lt;/span&gt;&lt;/label&gt;
      &lt;input type="text" [(ngModel)]="username" name="username"  class="form-control" placeholder="JDoe2255"&gt;
    &lt;/div&gt;
  
    &lt;div class="form-group"&gt;
      &lt;label&gt;Password: &lt;span&gt;*&lt;/span&gt;&lt;/label&gt;
      &lt;input type="password" [(ngModel)]="password" name="password"  class="form-control" placeholder="*****"&gt;
    &lt;/div&gt;
  
    &lt;div class="form-group"&gt;
      &lt;label&gt;E-mail:&lt;/label&gt;
      &lt;input type="text" [(ngModel)]="email" name="email"  class="form-control" placeholder="J.Doe@example.com"&gt;
    &lt;/div&gt;
  
    &lt;div class="form-group"&gt;
      &lt;label&gt;Phone Number: &lt;span&gt;*&lt;/span&gt;&lt;/label&gt;
      &lt;input type="text" [(ngModel)]="phone" name="phone"  class="form-control" placeholder="555-555-5555"&gt;
    &lt;/div&gt;
  
    &lt;div class="form-group"&gt;
      &lt;label&gt;State:&lt;span&gt;*&lt;/span&gt;&lt;/label&gt;
      &lt;input type="text" [(ngModel)]="state" name="state"  class="form-control" placeholder="Georgia"&gt;
    &lt;/div&gt;
  
    &lt;div class="form-group"&gt;
      &lt;label&gt;City:&lt;span&gt;*&lt;/span&gt;&lt;/label&gt;
      &lt;input type="text" [(ngModel)]="city" name="city"  class="form-control" placeholder="Atlanta"&gt;
    &lt;/div&gt;
  
    &lt;div class="form-group"&gt;
      &lt;label&gt;Years of Experience:&lt;span&gt;*&lt;/span&gt;&lt;/label&gt;
      &lt;input type="text" [(ngModel)]="experience" name="experience"  class="form-control" placeholder="5"&gt;
    &lt;/div&gt;
  
    &lt;div class="form-group"&gt;
      &lt;label&gt;Specialty:&lt;span&gt;*&lt;/span&gt;&lt;/label&gt;
      &lt;input type="text" [(ngModel)]="specialty" name="specialty"  class="form-control" placeholder="Er Nurse"&gt;
    &lt;/div&gt;
  
    &lt;div class="form-group"&gt;
      &lt;label&gt;Desired pay:&lt;span&gt;*&lt;/span&gt;&lt;/label&gt;
      &lt;input type="text" [(ngModel)]="wages" name="wages"  class="form-control" placeholder="In hourly rate"&gt;
      &lt;!--&lt;label&gt;&lt;input type="radio" [(ngModel)]="negotiable" name="negotiable"  value=""&gt;&amp;nbsp; Negotiable&lt;/label&gt;--&gt;
    &lt;/div&gt;
  
    &lt;input type="submit" class="btn btn-primary text-right" value="Submit"&gt;
  
  &lt;/form&gt;
  </code></pre>