请稍等 ...
×

采纳答案成功!

向帮助你的同学说点啥吧!感谢那些助人为乐的人

报错 err is not defined

function querySql(sql) {
    const conn = connect();
    return new Promise((resolve, reject) => {
        try {
            conn.query(sql, (err, results => {
                if (err) {
                    reject(err)
                } else {
                    resolve(results)
                }

            }))
        } catch (e) {
            reject(e)
        } finally {
            conn.end(); // 不释放的话,会造成内存泄露
        }
    })
}

报错 err is not defined

正在回答 回答被采纳积分+3

1回答

提问者 幕布斯1065030 2022-05-29 16:04:53

POST /users/login 200 105.378 ms - 31

(node:1876) UnhandledPromiseRejectionWarning: ReferenceError: err is not defined

    at H:\desktop\learn\learnnode\myapp\db\index.js:27:30

    at new Promise (<anonymous>)

    at querySql (H:\desktop\learn\learnnode\myapp\db\index.js:25:12)

    at H:\desktop\learn\learnnode\myapp\routes\users.js:22:3

    at Layer.handle [as handle_request] (H:\desktop\learn\learnnode\myapp\node_modules\_express@4.16.4@express\lib\router\layer.js:95:5)     

    at next (H:\desktop\learn\learnnode\myapp\node_modules\_express@4.16.4@express\lib\router\route.js:137:13)

    at Route.dispatch (H:\desktop\learn\learnnode\myapp\node_modules\_express@4.16.4@express\lib\router\route.js:112:3)

    at Layer.handle [as handle_request] (H:\desktop\learn\learnnode\myapp\node_modules\_express@4.16.4@express\lib\router\layer.js:95:5)     

    at H:\desktop\learn\learnnode\myapp\node_modules\_express@4.16.4@express\lib\router\index.js:281:22

    at Function.process_params (H:\desktop\learn\learnnode\myapp\node_modules\_express@4.16.4@express\lib\router\index.js:335:12)

(Use `node --trace-warnings ...` to show where the warning was created)

(node:1876) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 2)

(node:1876) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.


0 回复 有任何疑惑可以回复我~
问题已解决,确定采纳
还有疑问,暂不采纳
意见反馈 帮助中心 APP下载
官方微信