egg.js在中间件中怎么使用mysql连接?
发布于 7 年前 作者 nobody 8010 次浏览 来自 问答

遇到的问题有两个: 1,config.default.js中配置了4个数据库连接,default:{}为空,agent:true,启动npm run dev.启动报错。ERROR 3764 [agent_worker] start error, exiting with code:1. 2,配置文件中agent:false,可以启动。但这个 const dbClient = this.mysql.get(‘db4’); Cannot read property ‘get’ of undefined. 请问@atian25是否使用姿势不对。

5 回复

跟 agent 没啥关系,报错信息要提供。 可以看下文档和 eggjs/examples 那边的示例,如果还有问题,提供最小可复现代码提 issue

吧mysql 的配置放入config.default.js里面就可以了。 ‘use strict’;

module.exports = appInfo => { const config = { mysql: { // 单数据库信息配置 client: { // host host: ‘localhost’, // 端口号 port: ‘3306’, // 用户名 user: ‘user’, // 密码 password: ‘password’, // 数据库名 database: ‘database’, }, // 是否加载到 app 上,默认开启 app: true, // 是否加载到 agent 上,默认关闭 agent: false, } };

// should change to your own
config.keys = appInfo.name + '_1502349398822_1164';

// add your config here

return config;

};

@nxycdl 是多数据源在“中间件”中使用mysql,不是一个库,config.default.js已经配置了mysql

@nobody 提供最小可复现案例,去 egg 那边提 issue 吧,我没看明白问题

this.app.mysql.get(‘db4’).xxx()

回到顶部