快递:未定义的等待功能

问题描述

我在执行 Executesql 函数后变得未定义。 login函数使用Executesql函数来检查用户是否存在?运行此文件时出现以下错误。异步等待。

[nodemon] 由于更改而重新启动... >[nodemon] 开始 America/New_York >未定义 >未定义 >指标1

node DL.js

解决方法

您没有从 ExecuteSQL 返回任何内容。此外,您不应将 async/await 与回调混合使用。并且您不应将 async/await.then() 混用。决定您更喜欢哪一个并坚持您的决定。

你可以这样做

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 手册。