gpt4 book ai didi

node.js - 错误 : Can't add new command when connection is in closed state

转载 作者:行者123 更新时间:2023-12-03 12:15:47 25 4
gpt4 key购买 nike

我最近在实时服务器上部署了我的 node.js API 应用程序。我在实时服务器上遇到这些问题。

我用谷歌搜索了它,但找不到任何确切的解决方案。谁能建议我如何解决这个问题?

{ Error: read ETIMEDOUT at TCP.onread (net.js:622:25) errno: 'ETIMEDOUT', code: 'ETIMEDOUT', syscall: 'read', fatal: true }

{ Error: Can't add new command when connection is in closed state at PoolConnection._addCommandClosedState }

我像这样使用mysql连接池
var mysql = require('mysql2');
var mysqlPool = mysql.createPool({
host: 'localhost',
user: 'root',
password: 'xyz',
database: 'xyz',
waitForConnections: true,
connectionLimit: 10,
queueLimit: 0
});
module.exports = mysqlPool;

最佳答案

我遇到了类似的问题,最终不得不将连接请求放在它自己的 .js 文件中并将其导入 Controller -
connectionRequest.js

module.exports = function () {

let mysql = require('mysql2')
let connCreds = require('./connectionsConfig.json');

//Establish Connection to the DB
let connection = mysql.createConnection({
host: connCreds["host"],
user: connCreds['username'],
password: connCreds['password'],
database: connCreds['database'],
port: 3306
});

//Instantiate the connection
connection.connect(function (err) {
if (err) {
console.log(`connectionRequest Failed ${err.stack}`)
} else {
console.log(`DB connectionRequest Successful ${connection.threadId}`)
}
});

//return connection object
return connection
}
一旦我这样做了,我就可以像这样将它导入到我对 Controller 文件的查询中
ControllerFile.js
let connectionRequest = require('../config/connectionRequest')

controllerMethod: (req, res, next) => {
//Establish the connection on this request
connection = connectionRequest()

//Run the query
connection.query("SELECT * FROM table", function (err, result, fields) {
if (err) {
// If an error occurred, send a generic server failure
console.log(`not successful! ${err}`)
connection.destroy();

} else {
//If successful, inform as such
console.log(`Query was successful, ${result}`)

//send json file to end user if using an API
res.json(result)

//destroy the connection thread
connection.destroy();
}
});
},

关于node.js - 错误 : Can't add new command when connection is in closed state,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55842623/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com