报错 err is not defined

来源:10-6 mysql查询开发

幕布斯1065030

2022-05-29

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

写回答

1回答

幕布斯1065030

提问者

2022-05-29

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
0

Vue Element+Node.js开发企业通用管理后台系统

基于Element的中后台课程,一套中小型企业通用的后台管理系统

2829 学习 · 1714 问题

查看课程