CNode

egg.js在中间件中怎么使用mysql连接?

问答
Nnobody发布于9 年前最后回复9 年前5 回复9558 浏览0 收藏

遇到的问题有两个: 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)

A
atian25#1·9 年前

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

N
nxycdl#2·9 年前

吧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;

};

N
nobody#3·9 年前
引用 nxycdl吧mysql 的配置放入config.default.js里面就可以了。 'use strict'; module.exports = appInfo = { const config = {...

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

A
atian25#4·9 年前

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

A
AnzerWall#5·9 年前

this.app.mysql.get('db4').xxx()

参与回复
登录后即可参与回复。登录