获得解析的json值

时间:2017-12-06 14:16:23

标签: json node.js parsing

我正在尝试利用bcrypt来检查我的数据库中保存的密码中的密码,但是json响应当前正在返回:

[
    {
        "password": "$2a$10$8/o1McdQ24pL5MU7dkbhmewkjne83M2duPKp0cb6uowWvOPS"
    }
]

这导致bcrypt比较出错,因为它正在比较整个响应,而不仅仅是它内部的哈希。如何才能在响应中获得哈希的值?以下是我的代码:

app.get('/checkHash/:username/:pass', function(req, res) {
    console.log('below is the data');
    console.log(req.params);
    var pass = req.params.pass

        var createPromise = interact.getHash(req.params.username);
        //did promise
        createPromise.then(function(createResponse) {  
            //below checks to see if the password value matches the hash
            if(bcrypt.compareSync(pass, createResponse)) {
                //this means that the hashes are the same for user login
                res.json("yes");
            } else {
                //this means that the password hashes didn't match
                res.json("no");
         } 
        }).catch(function(err) {
        console.log(err);
    });
});

1 个答案:

答案 0 :(得分:1)

你的回答显然是一个对象数组。如果您只想比较数组的第一个结果,则必须将索引传递给数组。

app.get('/checkHash/:username/:pass', function(req, res) {
    console.log('below is the data');
    console.log(req.params);
    var pass = req.params.pass

        var createPromise = interact.getHash(req.params.username);
        //did promise
        createPromise.then(function(createResponse) {  
            //below checks to see if the password value matches the hash
            if(bcrypt.compareSync(pass, createResponse[0].password)) {
                //this means that the hashes are the same for user login
                res.json("yes");
            } else {
                //this means that the password hashes didn't match
                res.json("no");
         } 
        }).catch(function(err) {
        console.log(err);
    });
});