Node.js 从数据库psql获取字符串
嗨,我需要帮助弄清楚如何将数据库中的字符串项放入变量中。我所有的代码都可以工作,如果我将数据库中的确切字符串复制并粘贴到我的bcrypt compare调用中,它就可以完美地工作。基本上,我的问题是我的变量“compare”打印为 [{“密码”:“$2a$10$eilJb6SGKSSQm2b.U5tj.ut4o8.ohyjnvhbkpjcppeomcj4glmeyqc”}] 但我只需要 “$2a$10$eilJb6SGKSSQm2b.U5tj.ut4o8.oHyJNVhbkpjcpeomCj4GlMEyqC”。有什么想法吗Node.js 从数据库psql获取字符串,node.js,database,string,psql,Node.js,Database,String,Psql,嗨,我需要帮助弄清楚如何将数据库中的字符串项放入变量中。我所有的代码都可以工作,如果我将数据库中的确切字符串复制并粘贴到我的bcrypt compare调用中,它就可以完美地工作。基本上,我的问题是我的变量“compare”打印为 [{“密码”:“$2a$10$eilJb6SGKSSQm2b.U5tj.ut4o8.ohyjnvhbkpjcppeomcj4glmeyqc”}] 但我只需要 “$2a$10$eilJb6SGKSSQm2b.U5tj.ut4o8.oHyJNVhbkpjcpeomCj4G
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!");
}
});
而不是
compare = JSON.stringify(result.rows, null, " ");
试一试
您不需要JSON字符串,只需要JavaScript值
compare = result.rows[0].password;