为什么我在 mysql 中遇到 Fatal Error with nodejs?
Why I face Fatal Error in mysql with nodejs?
我正在尝试在 nodejs 中使用 mysql 模块创建数据库,但是在我创建 table 并插入一些数据后,当我尝试 SELECT 并取回结果时,我遇到致命错误!!
D:\Programming\Farahani App\App>node mysql.js
Error: Cannot enqueue Query after fatal error.
at Protocol._validateEnqueue (D:\Programming\Farahani App\App\node_modules\mysql\lib\protocol\Protocol.js:212:16)
at Protocol._enqueue (D:\Programming\Farahani App\App\node_modules\mysql\lib\protocol\Protocol.js:138:13)
at Connection.query (D:\Programming\Farahani App\App\node_modules\mysql\lib\Connection.js:198:25)
at Handshake.<anonymous> (D:\Programming\Farahani App\App\mysql.js:28:13)
at Handshake.<anonymous> (D:\Programming\Farahani App\App\node_modules\mysql\lib\Connection.js:526:10)
at Handshake._callback (D:\Programming\Farahani App\App\node_modules\mysql\lib\Connection.js:488:16)
at Handshake.Sequence.end (D:\Programming\Farahani App\App\node_modules\mysql\lib\protocol\sequences\Sequence.js:83:24)
at Protocol.handleNetworkError (D:\Programming\Farahani App\App\node_modules\mysql\lib\protocol\Protocol.js:369:14)
at Connection._handleNetworkError (D:\Programming\Farahani App\App\node_modules\mysql\lib\Connection.js:418:18)
at Socket.emit (node:events:390:28) {
code: 'PROTOCOL_ENQUEUE_AFTER_FATAL_ERROR',
fatal: false
}
undefined
[mysql 在我的 xampp 中是 运行 如果相关...]
感谢您的帮助。
我的代码:
const mysql = require('mysql');
const data = {
host: "localhost",
user: "user",
password: "password",
database: "mydb"
}
const con = mysql.createConnection(data)
con.connect(() => {
// create database
con.query('CREATE DATABASE mydb', () => {
})
// create table
const table = "CREATE TABLE customers (name VARCHAR(255), address VARCHAR(255))";
con.query(table, () => {
})
// add val to table
const val1 = "INSERT INTO customers (name, address) VALUES ('Company Inc', 'Highway 37')";
con.query(val1, () => {
})
// log results
con.query("SELECT * FROM customers", (err,result)=>{
if(err) {console.log(err)};
console.log(result);
})
con.end();
})
您的代码有两个问题。主要问题是您不能在 mysql.
中对每个连接执行多个查询
您可以使用 connection pools 使其更易于管理。
其次,您应该检查回调中的错误。这将有助于调试并使您的代码更健壮。
con.query("select * from my_table", (error, results, fields) => {
if (error) throw error;
console.log(results);
});
const mysql = require('mysql');
const con = mysql.createPool({
host: "localhost",
user: "user",
password: "password",
database: "mydb"
});
// create database
con.query('CREATE DATABASE mydb', (err) => {
if(err) {console.log(err)};
})
// create table
const table = "CREATE TABLE customers (name VARCHAR(255), address VARCHAR(255))";
con.query(table, (err) => {
if(err) {console.log(err)};
})
// add val to table
const val1 = "INSERT INTO customers (name, address) VALUES ('Company Inc', 'Highway 37')";
con.query(val1, (err) => {
if(err) {console.log(err)};
})
// log results
con.query("SELECT * FROM customers", (err,result)=>{
if(err) {console.log(err)};
console.log(result);
})
我正在尝试在 nodejs 中使用 mysql 模块创建数据库,但是在我创建 table 并插入一些数据后,当我尝试 SELECT 并取回结果时,我遇到致命错误!!
D:\Programming\Farahani App\App>node mysql.js
Error: Cannot enqueue Query after fatal error.
at Protocol._validateEnqueue (D:\Programming\Farahani App\App\node_modules\mysql\lib\protocol\Protocol.js:212:16)
at Protocol._enqueue (D:\Programming\Farahani App\App\node_modules\mysql\lib\protocol\Protocol.js:138:13)
at Connection.query (D:\Programming\Farahani App\App\node_modules\mysql\lib\Connection.js:198:25)
at Handshake.<anonymous> (D:\Programming\Farahani App\App\mysql.js:28:13)
at Handshake.<anonymous> (D:\Programming\Farahani App\App\node_modules\mysql\lib\Connection.js:526:10)
at Handshake._callback (D:\Programming\Farahani App\App\node_modules\mysql\lib\Connection.js:488:16)
at Handshake.Sequence.end (D:\Programming\Farahani App\App\node_modules\mysql\lib\protocol\sequences\Sequence.js:83:24)
at Protocol.handleNetworkError (D:\Programming\Farahani App\App\node_modules\mysql\lib\protocol\Protocol.js:369:14)
at Connection._handleNetworkError (D:\Programming\Farahani App\App\node_modules\mysql\lib\Connection.js:418:18)
at Socket.emit (node:events:390:28) {
code: 'PROTOCOL_ENQUEUE_AFTER_FATAL_ERROR',
fatal: false
}
undefined
[mysql 在我的 xampp 中是 运行 如果相关...]
感谢您的帮助。
我的代码:
const mysql = require('mysql');
const data = {
host: "localhost",
user: "user",
password: "password",
database: "mydb"
}
const con = mysql.createConnection(data)
con.connect(() => {
// create database
con.query('CREATE DATABASE mydb', () => {
})
// create table
const table = "CREATE TABLE customers (name VARCHAR(255), address VARCHAR(255))";
con.query(table, () => {
})
// add val to table
const val1 = "INSERT INTO customers (name, address) VALUES ('Company Inc', 'Highway 37')";
con.query(val1, () => {
})
// log results
con.query("SELECT * FROM customers", (err,result)=>{
if(err) {console.log(err)};
console.log(result);
})
con.end();
})
您的代码有两个问题。主要问题是您不能在 mysql.
中对每个连接执行多个查询您可以使用 connection pools 使其更易于管理。
其次,您应该检查回调中的错误。这将有助于调试并使您的代码更健壮。
con.query("select * from my_table", (error, results, fields) => {
if (error) throw error;
console.log(results);
});
const mysql = require('mysql');
const con = mysql.createPool({
host: "localhost",
user: "user",
password: "password",
database: "mydb"
});
// create database
con.query('CREATE DATABASE mydb', (err) => {
if(err) {console.log(err)};
})
// create table
const table = "CREATE TABLE customers (name VARCHAR(255), address VARCHAR(255))";
con.query(table, (err) => {
if(err) {console.log(err)};
})
// add val to table
const val1 = "INSERT INTO customers (name, address) VALUES ('Company Inc', 'Highway 37')";
con.query(val1, (err) => {
if(err) {console.log(err)};
})
// log results
con.query("SELECT * FROM customers", (err,result)=>{
if(err) {console.log(err)};
console.log(result);
})