创建数据库连接池报错:create:function(callback){} error:callback is not a function

标签: 开发求助
wangling 在 6-19 15:56发布
wangling 在 6-19 15:58重新编辑 分享到 weibo
2 回复
#1 reerayxia 6-28 14:18 回复

才看到有人提这个问题
已经解决,是库升级的问题。
代码参考如下:
1.mysql.js中的NND修改如下

NND.query = function (sql, args, cb) {
const resourcePromise = _pool.acquire();
resourcePromise.then(function (client) {
client.query(sql, args, function (err, res) {
_pool.release(client);
cb(err, res);
});
}).catch(function (err) {
// handle error - this is generally a timeout or maxWaitingClients
// error
console.log("errr... query", err);
cb(err);
});
};

2..pool文件如下

var genericPool = require('generic-pool');
var mysql = require("mysql");

/*
* Create mysql connection pool.
*/
var createMysqlPool = function (app) {
var mysqlConfig = app.get('mysql');

const factory = {
    create: function () {
        return new Promise(function (resolve, reject) {

            var client = mysql.createConnection({
                host: mysqlConfig.host,
                user: mysqlConfig.user,
                password: mysqlConfig.password,
                database: mysqlConfig.database
            });
            client.connect();
            resolve(client);
        });
    },
    destroy: function (client) {
        return new Promise(function (resolve) {
            client.on('end', function () {
                resolve();
            });
            client.disconnect();
        })
    }
};

var opts = {
    max: 10, // maximum size of the pool
    min: 1 // minimum size of the pool
};


return genericPool.createPool(factory, opts);

};
exports.createMysqlPool = createMysqlPool;

#2 wangling 7-14 10:54 回复

已解决,谢谢

回到顶部