You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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: 'testuser@test.com'
}
}).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.
The text was updated successfully, but these errors were encountered:
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?!
@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😨
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
auth/local/local.controller.js
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.
The text was updated successfully, but these errors were encountered: