Javascript Express:未定义的等待函数
在执行ExecuteSQL函数后,我变得未定义。login函数使用ExecuteSQL函数检查用户是否存在?运行此文件时出现以下错误。异步等待 [nodemon]由于更改而重新启动。。。 >[nodemon]正在启动Javascript Express:未定义的等待函数,javascript,node.js,sql-server,async-await,tedious,Javascript,Node.js,Sql Server,Async Await,Tedious,在执行ExecuteSQL函数后,我变得未定义。login函数使用ExecuteSQL函数检查用户是否存在?运行此文件时出现以下错误。异步等待 [nodemon]由于更改而重新启动。。。 >[nodemon]正在启动节点DL.js >未定义 >未定义 >指标1 您没有从ExecuteSQL返回任何内容。此外,您不应该将async/await与回调混合使用。你不应该把async/await和混在一起。然后()决定你更喜欢哪一个,并坚持你的决定 你可以这么做 async function Execu
节点DL.js
>未定义
>未定义
>指标1
您没有从ExecuteSQL返回任何内容。此外,您不应该将
async/await
与回调混合使用。你不应该把async/await
和混在一起。然后()
决定你更喜欢哪一个,并坚持你的决定
你可以这么做
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);
}
};
您永远不应该使用字符串连接或字符串模板来构造查询,因为这非常容易出错且不安全。有关如何创建参数化查询,请参阅mssql手册
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);
}
};