I'm getting undefined after executing the ExecuteSQL function. The login function uses the ExecuteSQL function is used to check whether a user exists? I'm getting the following error while I'm running this file. async await.
[nodemon] restarting due to changes... >[nodemon] starting
node DL.js
>undefined >undefined >indicator1
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(function (data) {
console.log(data);
return data
});
console.log(result);
console.log('indicator1')
} catch (err) {
console.log(err);
}
};
login('ADMIN');
You are not returning anything from ExecuteSQL. Furthermore, you should not mix async/await
with callbacks. And you should not mix async/await
with .then()
Decide which one you like better and stick with your decision.
You can just do
async function ExecuteSQL(strSQL) {
try {
const pool = await getConnection();
//you don't need to check for the pool, because getConnection() will throw if there is an error
const result = await pool.request().query(strSQL);
return result;
} catch (err) {
console.log(err);
}
};
async function login(strUID) {
const strSQL = `SELECT fUserPwd FROM tblUser WHERE fUserID ='${strUID}'`;
try {
const result = await ExecuteSQL(strSQL);
console.log(result);
return result;
} catch (err) {
console.log(err);
}
};
And you should NEVER construct your queries with string concatenation or string templates because that's very error prone and insecure. See the mssql manual on how to create parameterized queries.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.