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'
let password = await (async function() {...} ())as you are creating a promise with theasyncyou need toawaitfor itasync, just markapp.postas anasyncmethod then you can useawaitfrom the start..app.post('/test', async function () {No need for the anonymous function.express.. The Promise is ignored, and you just handle the errors inside,.. I'll post an example..