当我用密码postman中的bcrypt执行“saveUser”功能时发送给我“无法得到任何回复”

时间:2017-08-17 15:57:04

标签: node.js mongodb cygwin postman

当我尝试测试用户的POST时,我收到邮件员中的“错误”“无法获得任何响应”:/ register,这是我的用户控制器代码,当我添加了bcrypt的哈希密码时,这是我在user.js(控制器)中调用的内容:

try{  
            // create a mysql database connection
            String myDriver = "org.gjt.mm.mysql.Driver";
            String myUrl = "jdbc:mysql://localhost/test";
            Class.forName(myDriver);
            Connection conn = DriverManager.getConnection(myUrl, "root", "");

            // create a sql date object so we can use it in our INSERT statement
            Calendar calendar = Calendar.getInstance();
            java.sql.Date startDate = new java.sql.Date(calendar.getTime().getTime());

            // the mysql insert statement
            String query = " insert into users (first_name, last_name, date_created, is_admin, num_points)"
                    + " values (?, ?, ?, ?, ?)";

            // create the mysql insert preparedstatement
            PreparedStatement preparedStmt = conn.prepareStatement(query);
            preparedStmt.setString (1, "Barney");
            preparedStmt.setString (2, "Rubble");
            preparedStmt.setDate   (3, startDate);
            preparedStmt.setBoolean(4, false);
            preparedStmt.setInt    (5, 5000);

            // execute the preparedstatement
            preparedStmt.execute();




            //now to get the boolean from Database
            Statement stmt=conn.createStatement();  
            ResultSet rs=stmt.executeQuery("select * from emp");  
            while(rs.next())  
                System.out.println(rs.getBoolean(4));  
            conn.close();  


        }catch(Exception e){ 
            System.out.println(e);
        } 

这是我想要获得POST的功能

var bcrypt = require('bcrypt-nodejs');
var User = require('../models/user');
var jwt = require('../services/jwt');

我将其导出:

function saveUser(req, res){
    var user = new User();

    var params = req.body;

    console.log(params);

    user.name = params.name;
    user.surname = params.surname;
    user.email = params.email;
    user.role = 'ROLE_USER';
    user.image = 'null';

    if(params.password){
        // Encriptar contraseña
        bcrypt.hash(params.password, null, null, function(err, hash){
            user.password = hash;

            if(user.name != null && user.surname != null && user.email != null){
                // Guarda el usuario
                user.save((err, userStored) => {
                    if(err){
                        res.status(500).send({message: 'Error al guardar el usuario'});
                    }else{
                        if(!userStored){
                            res.status(404).send({message: 'Noseha registrado el usuario'});
                        }else{
                            res.status(200).send({user: userStored});
                        }
                    }
                });
            }else{
                res.status(200).send({message: 'Rellena todos los campos'});
            }
        });
    }else{
        res.status(200).send({message: 'Introduce la contraseña'});
    }
}

我在我的文件夹路线中完成了路线:

module.exports = {
    saveUser
};

我的模特是:

var express = require('express');
var UserController = require('../controllers/user');

var api = express.Router();

api.post('/register', UserController.saveUser);

module.exports = api;

所以我有一切,但应用程序没有运行,在我的cygwin终端它给我响应,但它不保存,因为功能“saveUser”不起作用,任何人都可以帮助我吗?

1 个答案:

答案 0 :(得分:0)

我找到了解决方案,我删除了mongoose最新版本并安装了4.10.8

在cygwin中:

npm install mongoose@4.10.8 --save