lordofPomelo 点注册web-server客户端出错了,请问怎么回事?

DaiDandeMacBook-Pro:web-server daidan$ node app
Web server has started.
Please log on http://127.0.0.1:3001/
TypeError: callback is not a function
at Object.create (/project/pomelo-project/lordofpomelo/web-server/lib/dao/mysql/dao-pool.js:23:7)
at Pool.createResource (/project/pomelo-project/lordofpomelo/web-server/node_modules/generic-pool/lib/Pool.js:279:42)
at Pool.
dispense (/project/pomelo-project/lordofpomelo/web-server/node_modules/generic-pool/lib/Pool.js:210:12)
at Pool.acquire (/project/pomelo-project/lordofpomelo/web-server/node_modules/generic-pool/lib/Pool.js:406:10)
at Object.NND.query (/project/pomelo-project/lordofpomelo/web-server/lib/dao/mysql/mysql.js:25:8)
at Object.userDao.createUser (/project/pomelo-project/lordofpomelo/web-server/lib/dao/userDao.js:38:9)
at /project/pomelo-project/lordofpomelo/web-server/app.js:81:11
at callbacks (/project/pomelo-project/lordofpomelo/web-server/node_modules/express/lib/router/index.js:272:11)
at param (/project/pomelo-project/lordofpomelo/web-server/node_modules/express/lib/router/index.js:246:11)
at pass (/project/pomelo-project/lordofpomelo/web-server/node_modules/express/lib/router/index.js:253:5)

标签: lordofpomelo
jtz0461 在 2-19 16:48发布 分享到 weibo
1 回复
#1 {1} wayne5ning 2-19 20:49 回复

我遇到的同样的问题,但是已经解决了,可以运行了。

原因:generic-pool最新版是用的es6了,使用的promise而不是回调。
我使用的解决办法是使用promise,参考generic-pool官方说明。主要修改有两处:

// lib/mysql/dao-pool.js中的createMysqlPool方法改为:
var createMysqlPool = function(){
  return _poolModule.createPool({
    name     : 'mysql',
    create: function(){
      return new Promise(function(resolve, reject){
        var client = mysql.createConnection({
          host: mysqlConfig.host,
          user: mysqlConfig.user,
          password: mysqlConfig.password,
          database: mysqlConfig.database
        });
        resolve(client);
      })
    },

    destroy  : function(client) {
      return new Promise(function(resolve){
        client.end();
      })
    }
  }, {
    max      : 10,
    idleTimeoutMillis : 30000,
    log : false
  });
};
// lib/mysql/mysql.js中的NND.query方法改为:
NND.query = function (sql, args, callback) {
    const resourcePromise = _pool.acquire();
    resourcePromise.then(function (client) {
        client.query(sql, args, function (err, res) {
            _pool.release(client);
            callback && callback(err, res);
        });
    })
    .catch(function (err) {
        console.error('[sqlqueryErr] ' + err.stack);
    });
};
wayne5ning 2-19 20:51 回复

值得注意的是,web-server和game-server中的这两个文件分别都要改,否则登录不了。前者管注册,后者管登录。

回到顶部