i follow this question but i still didn't figure out how to solve it, it says to use instanceMethods option to use the methods in the model thats exactly what i did:
"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) {
console.log("hi");
return bcrypt.hashSync(password, bcrypt.genSaltSync(8), null);
},
validPassword: function (password) {
return bcrypt.compareSync(password, this.password);
}
},
});
return User;
}
after i use that i tried to test it generating a password hash on my register route like this:
var express = require('express');
var User = require('../../models').User;
var router = express.Router();
/* GET users listing. */
router.post('/', function (req, res, next) {
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" });
}
var password = User.instaceMethods.generateHash(req.body.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;
with this when i go to my /register, i don't get any response even a console.log in the password doesn't show me anything i tried to see if it enters the generetehash but, it doesn't enter the method, what is going wrong here?
via Antonio Costa
No comments:
Post a Comment