Click here to Skip to main content
15,891,905 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am using nodejs + postgreSQL to do the registration of my app, so i have this basic model:

<pre lang="Javascript">"use strict";
var sequelize = require('./index');
var bcrypt = require('bcryptjs');

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;
};

it worked before when i didn't had the password, now i want to hash that password and salt, so in my register route i have this:

JavaScript
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.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;

now i just don't get any response of my postman and the row isn't saving on the databse any sugestion?

What I have tried:

I tried everything searched a lot about the problem,
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900