I am trying to query mssql database to get password for the user, that is sending post request and then just show it in console. But the promise I've made isn't working the way I wanted to and the password isn't loaded into the variable. Here's my code:
app.post('/test', function () {
let user = 'User';
let query = 'SELECT [Password] as password FROM [Table] where [User] = ' + SqlString.escape(user);
let password = (async function () {
try {
let pool = await sql.connect(dbConfig);
let result = await pool.request()
.query(querys);
console.dir(result.recordset[0].password) //Value here is OK
return result.recordset[0].password
} catch (err) {
// ... error checks
}
})()
console.log(password); //here I am getting "Promise { pending }"
});
The result I get is: Promise { pending } 'DatabasePassword'