您好我需要帮助找出如何将我的数据库中的字符串条目输入变量。我的所有代码都有效,如果我将数据库中的确切字符串复制并粘贴到我的bcrypt比较调用中,它就能完美运行。基本上我的问题是我的变量“compare”打印出来
[{“password”:“$ 2a $ 10 $ eilJb6SGKSSQm2b.U5tj.ut4o8.oHyJNVhbkpjcpeomCj4GlMEyqC”}]
但我只需要
“$ 2A $ 10 $ eilJb6SGKSSQm2b.U5tj.ut4o8.oHyJNVhbkpjcpeomCj4GlMEyqC”。有什么想法吗?
account.on("end", function(result) {
if(result.rows.length > 0){
console.log("found account: ");
console.log(JSON.stringify(result.rows, null, " "));
//turn the db entry into a string
compare = JSON.stringify(result.rows, null, " ");
//compare the hashed password from database to parameter for password change
bcrypt.compare(password, compare, function(err, res) {
console.log("res result: " + res);
if(res){
console.log("passwords are same");
//new query updating the password in the db
if(person=="s"){
//update db
client.query('UPDATE students SET password=($2) WHERE email=($1)',
[req.body.email, hash_new]);
console.log("changed students password");
} else {
//update db
client.query('UPDATE teachers SET password=($2) WHERE email=($1)',
[req.body.email, hash_new]);
console.log("changed teacher password");
}
} else {
console.log("passwords are not the same");
}
});
} else {
console.log("account not found!");
}
});
答案 0 :(得分:0)
而不是
compare = JSON.stringify(result.rows, null, " ");
试
compare = result.rows[0].password;
您不需要JSON字符串,但需要JavaScript值。