Skip to content

Instantly share code, notes, and snippets.

@wsfuller
Last active October 19, 2016 04:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wsfuller/8c38bba0c3a556bc534a0d7e6ecfcebe to your computer and use it in GitHub Desktop.
Save wsfuller/8c38bba0c3a556bc534a0d7e6ecfcebe to your computer and use it in GitHub Desktop.
var express = require('express');
var router = express.Router();
var jwt = require('jsonwebtoken');
var bcrypt = require('bcryptjs');
var hash = 'superSecret';
var config = require('../config/database');
User = require('../models/user.model.js');
// Create new User
router.post('/', function(req, res){
var email = req.body.email;
var password = req.body.password;
var user = new User({
email: email,
password: password
});
// Pre hook will take care of password creation
return user.save()
.then(function(user) {
// return user successful and token
console.log('get ready to prepare the token');
});
});
@sbaidon
Copy link

sbaidon commented Oct 17, 2016

You need to make an instance of a user you cannot call the save method on the model

 var express     = require('express');
 var router      = express.Router();
 var jwt         = require('jsonwebtoken'); 
 var bcrypt      = require('bcryptjs');
 var hash        = 'superSecret';
 var config      = require('../config/database');

 User = require('../models/user.model.js');

  // Create new User
  router.post('/', function(req, res){
      var { password, email } = req.body;
      var user = new User({
        name: name,
        password: password
      });
      // Pre hook will take care of password creation
      return user.save()
      .then(function(user) {
        console.log('routes user', user);
        return res.status(201).json({user, message: 'User was succesfully created'});
      });
    });

@wsfuller
Copy link
Author

Ok so passing a user object now back into the Model

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment