MySQL使Express服务器崩溃,无法处理错误

mysql crashes express server, not handling errors

提问人:Amber Ahmed 提问时间:4/24/2023 最后编辑:Amber Ahmed 更新时间:4/24/2023 访问量:40

问:

我刚刚用 Express 启动了 MySQL。我正在尝试使用书籍 ID 从 MySQL 数据库中获取书籍详细信息,如果书籍 ID 以整数形式给出,它会显示结果,但是当书籍 ID 未定义时,它会使服务器崩溃,而不是使用 try-catch 处理错误。 连接DB.js文件(用于连接数据库)

const pool = mysql.createPool({
  connectionLimit: 10,
  multipleStatements: true,
  host: "localhost",
  user: "root",
  password: "password",
  database: "library_management",
  port: 3306,
});
pool.getConnection((error, connection) => {
  if (error) throw error;
  console.log("connected to db");
  connection.release();
});
export default pool;

书籍.js文件(路由器)

import express from "express";
import pool from "../connectDB.js";
const booksRouter = express.Router();
booksRouter.get("/book/:bookId", async (req, res) => {
  try {
    const { bookId } = req.params;

    const query = `select * from books where book_id = ${bookId};`;
    pool.query(query, (error, result) => {
      if (error) throw error;
      res
        .status(200)
        .json({ access: true, msg: "fetched book details", bookDetails: result[0] });
    });
  } catch (error) {
     console.log(error);
    res.status(500).json({ access: false, error: "server error" });
  }
});

上面的代码是崩溃的服务器,如果书籍 ID 未定义,则不处理错误。而且,如果我将表名从书籍更改为书籍(不存在),那么它也会使服务器崩溃。

/home/amber/full-stack-web-dev/mini-projects/library-management-mysql/server/node_modules/mysql/lib/protocol/Parser.js:437
      throw err; // Rethrow non-MySQL errors
      ^

Error: ER_BAD_FIELD_ERROR: Unknown column 'undefined' in 'where clause'
    at Sequence._packetToError (/home/amber/full-stack-web-dev/mini-projects/library-management-mysql/server/node_modules/mysql/lib/protocol/sequences/Sequence.js:47:14)
    at Query.ErrorPacket (/home/amber/full-stack-web-dev/mini-projects/library-management-mysql/server/node_modules/mysql/lib/protocol/sequences/Query.js:79:18)
    at Protocol._parsePacket (/home/amber/full-stack-web-dev/mini-projects/library-management-mysql/server/node_modules/mysql/lib/protocol/Protocol.js:291:23)
    at Parser._parsePacket (/home/amber/full-stack-web-dev/mini-projects/library-management-mysql/server/node_modules/mysql/lib/protocol/Parser.js:433:10)
    at Parser.write (/home/amber/full-stack-web-dev/mini-projects/library-management-mysql/server/node_modules/mysql/lib/protocol/Parser.js:43:10)
    at Protocol.write (/home/amber/full-stack-web-dev/mini-projects/library-management-mysql/server/node_modules/mysql/lib/protocol/Protocol.js:38:16)
    at Socket.<anonymous> (/home/amber/full-stack-web-dev/mini-projects/library-management-mysql/server/node_modules/mysql/lib/Connection.js:88:28)
    at Socket.<anonymous> (/home/amber/full-stack-web-dev/mini-projects/library-management-mysql/server/node_modules/mysql/lib/Connection.js:526:10)
    at Socket.emit (node:events:513:28)
    at addChunk (node:internal/streams/readable:324:12)
    --------------------
    at Pool.query (/home/amber/full-stack-web-dev/mini-projects/library-management-mysql/server/node_modules/mysql/lib/Pool.js:199:23)
    at file:///home/amber/full-stack-web-dev/mini-projects/library-management-mysql/server/controllers/books.js:10:10
    at Layer.handle [as handle_request] (/home/amber/full-stack-web-dev/mini-projects/node_modules/express/lib/router/layer.js:95:5)
    at next (/home/amber/full-stack-web-dev/mini-projects/node_modules/express/lib/router/route.js:144:13)
    at Route.dispatch (/home/amber/full-stack-web-dev/mini-projects/node_modules/express/lib/router/route.js:114:3)
    at Layer.handle [as handle_request] (/home/amber/full-stack-web-dev/mini-projects/node_modules/express/lib/router/layer.js:95:5)
    at /home/amber/full-stack-web-dev/mini-projects/node_modules/express/lib/router/index.js:284:15
    at param (/home/amber/full-stack-web-dev/mini-projects/node_modules/express/lib/router/index.js:365:14)
    at param (/home/amber/full-stack-web-dev/mini-projects/node_modules/express/lib/router/index.js:376:14)
    at Function.process_params (/home/amber/full-stack-web-dev/mini-projects/node_modules/express/lib/router/index.js:421:3) {
  code: 'ER_BAD_FIELD_ERROR',
  errno: 1054,
  sqlMessage: "Unknown column 'undefined' in 'where clause'",
  sqlState: '42S22',
  index: 0,
  sql: 'select * from books where book_id = undefined;'
}

Node.js v18.12.1
[nodemon] app crashed - waiting for file changes before starting...
MySQL 节点 .js 快速 错误处理

评论

0赞 Phil 4/24/2023
使用占位符格式将值传递到查询中... 。至于值是,你在客户端使用什么 URL?pool.query("select * from books where book_id = ?", [bookId])undefined
0赞 Phil 4/24/2023
这回答了你的问题吗?Node-MySQL - 在 Query() 方法中转义 vs Mysql.Escape() / Mysql.EscapeId()
0赞 Amber Ahmed 4/30/2023
@Phil我询问如何处理错误,此方法也会抛出错误,然后服务器将再次崩溃。我正在使用 get 方法和 bookId 从 url 的参数中检索。

答: 暂无答案