Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sequelize update/save not working #1556

Closed
daniel7912 opened this issue Jan 13, 2016 · 3 comments
Closed

Sequelize update/save not working #1556

daniel7912 opened this issue Jan 13, 2016 · 3 comments

Comments

@daniel7912
Copy link

Hi,

I am trying to separate out the auth/local code a bit more and have created a new file, local.controller.js which contains all of the route functions. The problem I have is that inside this local.controller.js file, model.find(), model.destroy() and model.count() all seem to work fine, but when I try model.update() or model.save(), express crashes without returning any errors. The 'beforeUpdate' hook is not called either.

Here is my code:

auth/local/index.js

'use strict';

import express from 'express';
import * as controller from './local.controller';
var router = express.Router();

router.post('/', controller.authenticate);
router.post('/reset-password', controller.resetPassword);

export default router;

auth/local/local.controller.js

'use strict';

import config from '../../config/environment';
import mail from '../../mail';
import jwt from 'jsonwebtoken';
import expressJwt from 'express-jwt';
import passport from 'passport';
import {User} from '../../sqldb';
import {signToken} from '../auth.service';

export function authenticate(req, res, next) {

  passport.authenticate('local', function(err, user, info) {
    var error = err || info;
    if (error) {
      return res.status(401).json(error);
    }
    if (!user) {
      return res.status(404).json({message: 'Something went wrong, please try again.'});
    }

    var token = signToken(user._id, user.role);
    res.json({ token });
  })(req, res, next)

};

export function resetPassword(req, res, next) {

  var passwordResetToken = String(req.body.passwordResetToken);
  var newPassword = String(req.body.password);

  jwt.verify(passwordResetToken, config.secrets.forgotPassword, function(err, data) {

    if (err) {
      console.log(err);
      return res.status(403).json({message: 'Invalid token.'});
    }

    User.find({
      where: {
        email: '[email protected]'
      }
    }).then(function(user) {

      user.password = newPassword;

      return user.save().then(function() {
        console.log('success');
        res.json({message: 'Password reset successfully.'});
      })
      .catch(function(err) {
        console.log(err);
      });

    }).catch(function(err) {
      console.log(err);
    });

  });

};

The JWT verifies OK, then the user is found OK, lets me update the user.password value OK, but crashes at the save() function, and neither catch() is called.

I have used the exact same find/save code in seed.js and it works fine so the syntax is correct.

Could someone tell me where I'm going wrong please?

Thanks for any help.

@daniel7912
Copy link
Author

Right I've found the problem and I'm completely confused by it! It appears that if I console.log any kind of object, the app crashes without an error. So in my code above, if I turn:

console.log(req.body)
into
console.log(JSON.stringify(req.body));

and the same with the data object...it works fine?!

Possibly related to nodejs/node#4261

@tylerdmace
Copy link

What happens when you use console.table(req.body)?

@karlkurzer
Copy link

@tylerdmace I keep on having the same problem node v4.2.6, it took me forever to find this post and I am grateful to get the console.log(JSON.stringify(OBJECT)) hint, hopefully this will be fixed soon on node LTS, otherwise my production server might go down at some point without any console.log😨

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

No branches or pull requests

4 participants