【发布时间】:2020-03-15 07:49:46
【问题描述】:
我正在我的 node.js 项目中使用 MySQL 数据库。我用 Knex 创建了一个对数据库的查询,没关系。但是当我尝试再查询一次时,我遇到了这个错误:
Error: Unable to acquire a connection
at Client_MySQL.acquireConnection
(C:\Users\Darek\Desktop\proj\node_modules\knex\lib\client.js:336:30)
at Runner.ensureConnection
这是我的knexfile.js:
const dotenv = require('dotenv');
dotenv.config();
module.exports = {
client: 'mysql',
connection: {
host: 'localhost',
user: process.env.MYSQL_USER,
password: process.env.MYSQL_PASS,
database: 'testDB'
}
};
然后我必须重新启动我的 npm。我搜索了这个问题的解决方案,但没有适合我的解决方案。
我又看到了这个错误:
(node:8428) [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.
这是一个发生错误的代码。它在我的用户模型中起作用:
module.exports.check = (number) => {
var bbb = 0;
return knex
.from('employ')
.select('ID')
.where('emp_number', '=', number)
.then((row) => {
bbb = row.length;
return(bbb);
})
.finally(() => {
knex.destroy();
})
};
还有这个函数的调用:
const numberExist = await User.check(req.body.number);
【问题讨论】:
-
嗨,德里克。您能否包含显示您连接到数据库的位置的代码?这是 Express 服务器吗?
-
是的。是快递服务器。我编辑了我放置一些新代码的问题