When using the login function that utilizes ExecuteSQL to validate user existence, an error occurs during file execution related to async await.
ReferenceError: Cannot access 'result' before initialization at /Users/naseefali/Documents/Projects/common_express_api/data/DL.js:100:25 at async login (/Users/naseefali/Documents/Projects/common_express_api/data/DL.js:99:24) SELECT fUserPwd FROM tblUser WHERE fUserID ='ADMIN' { recordsets: [ [ [Object] ] ], recordset: [ { fUserPwd: '006060061500675006630067300667' } ], output: {}, rowsAffected: [ 1 ] }
Code
async function testConnection() {
try {
const pool = await getConnection();
if (pool) {
const result = await pool.request()
.query('SELECT * FROM tblUser', function (err, sqlResult) {
if (err) {
console.log(err);
}
else {
console.log(sqlResult);
}
});
}
else console.log(pool);
} catch (err) {
console.log(err);
}
};
async function ExecuteSQL(strSQL) {
try {
const pool = await getConnection();
if (pool) {
const result = await pool.request()
.query(strSQL, async function (err, sqlResult) {
if (err) {
console.log(err);
}
else {
console.log(strSQL);
console.log(sqlResult);
return sqlResult;
}
});
}
else console.log(pool);
} catch (err) {
console.log(err);
}
};
async function login(strUID) {
const strSQL = `SELECT fUserPwd FROM tblUser WHERE fUserID ='${strUID}'`;
try {
const result = await ExecuteSQL(strSQL).then(await function () {
console.log(result);
});
} catch (err) {
console.log(err);
}
};
login('ADMIN');