I am having a issue trying to hash password using bcrypt-nodejs with sequelize, when i route to my register i don't get any response and no register is added to my database :/.
So my model is this:
"use strict";
var sequelize = require('./index');
var bcrypt = require('bcrypt-nodejs');
module.exports = function (sequelize, DataTypes) {
var User = sequelize.define("User", {
username: DataTypes.STRING,
email: DataTypes.STRING,
password: DataTypes.STRING
}, {
instanceMethods: {
generateHash: function (password) {
return bcrypt.hashSync(password, bcrypt.genSaltSync(8), null);
},
validPassword: function (password) {
return bcrypt.compareSync(password, this.password);
}
},
});
return User;
}
on my register route i try to hash the password and assing it to my password field like this:
var express = require('express');
var User = require('../../models').User;
var router = express.Router();
/* GET users listing. */
router.post('/', function (req, res, next) {
console.log("hi");
if (JSON.stringify(req.body) == "{}") {
return res.status(400).json({ Error: "Register request body is empty" });
}
if (!req.body.email || !req.body.username || !req.body.password) {
return res.status(400).json({ Error: "Missing fields for registration" });
}
console.log(req.body.password);
var password = User.generateHash(req.body.password);
console.log(password);
User.create({
username: req.body.username,
email: req.body.email,
password: password
}).then(function () {
return res.status(200).json({message: "user created"});
})
});
module.exports = router;
as i said i don't get any response from postman to the register route and nothing get added in the database, the problem just occurs when i add password, without the password field, the register get added :/.
thank you :)
via Antonio Costa
No comments:
Post a Comment