Node js - Please could you help me to figure out how to retrieve a value from a query, inside an async function? The async function will take one parameter as input; and the sql result should be the output.
I've tried the code below but it provides 'undefined' as a result. I'm using MSSQL db.
Any tips would be appreciated. Thanks in advance.
getId('Justin')
.then( (mydata)=>{ console.log(mydata) } ); //I hope it to display the id of 'Justin'. But instead, it returns undefined
async function getId(inputName){
try{
let pool = await sql.connect(config); //ok
await pool.request()
.input('inputName', inputName )
.query( 'select id from mytable where inputName = @inputName' , (err,result)=>{
console.log (result.recordset[0].id); //here it works. it can display the id
return result.recordset[0].id; // but I could not return the id from here.
} )
}
catch(err){console.log(err)}
}