从数据库 psql 获取字符串
getting string from database psql
您好,我需要帮助弄清楚如何将数据库中的字符串条目放入变量中。我的所有代码都有效,如果我将数据库中的确切字符串复制并粘贴到我的 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=() WHERE email=()',
[req.body.email, hash_new]);
console.log("changed students password");
} else {
//update db
client.query('UPDATE teachers SET password=() WHERE email=()',
[req.body.email, hash_new]);
console.log("changed teacher password");
}
} else {
console.log("passwords are not the same");
}
});
} else {
console.log("account not found!");
}
});
而不是
compare = JSON.stringify(result.rows, null, " ");
尝试
compare = result.rows[0].password;
您不需要 JSON 字符串,而是 JavaScript 值。
您好,我需要帮助弄清楚如何将数据库中的字符串条目放入变量中。我的所有代码都有效,如果我将数据库中的确切字符串复制并粘贴到我的 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=() WHERE email=()',
[req.body.email, hash_new]);
console.log("changed students password");
} else {
//update db
client.query('UPDATE teachers SET password=() WHERE email=()',
[req.body.email, hash_new]);
console.log("changed teacher password");
}
} else {
console.log("passwords are not the same");
}
});
} else {
console.log("account not found!");
}
});
而不是
compare = JSON.stringify(result.rows, null, " ");
尝试
compare = result.rows[0].password;
您不需要 JSON 字符串,而是 JavaScript 值。