typescript egg mysql_Egg.js egg-mysql 连接MySQL 报 AppWorkerDiedError 错误

在使用Egg.js、TypeScript和egg-mysql进行开发时,遇到启动应用报AgentWorkerDiedError错误,具体为无法在插件中找到'enable'属性。错误源于egg-mysql配置问题,可能由于配置文件写法不正确或者版本兼容性导致。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

问题描述

> egg-bin dev

[egg-ts-helper] create typings/app/controller/index.d.ts (3ms)

[egg-ts-helper] create typings/config/index.d.ts (36ms)

[egg-ts-helper] create typings/app/index.d.ts (0ms)

2020-02-27 22:51:19,094 INFO 3754 [master] node version v10.16.3

2020-02-27 22:51:19,096 INFO 3754 [master] egg version 2.26.0

/Users/zhangbing/github/CodeTest/react_blog/service/node_modules/egg-core/lib/loader/mixin/plugin.js:207

if (!('enable' in plugin)) {

^

TypeError: Cannot use 'in' operator to search for 'enable' in egg-mysql

at AgentWorkerLoader.normalizePluginConfig (/Users/zhangbing/github/CodeTest/react_blog/service/node_modules/egg-core/lib/loader/mixin/plugin.js:207:20)

at AgentWorkerLoader.readPluginConfigs (/Users/zhangbing/github/CodeTest/react_blog/service/node_modules/egg-core/lib/loader/mixin/plugin.js:182:14)

at AgentWorkerLoader.loadPlugin (/Users/zhangbing/github/CodeTest/react_blog/service/node_modules/egg-core/lib/loader/mixin/plugin.js:62:29)

at AgentWorkerLoader.loadConfig (/Users/zhangbing/github/CodeTest/react_blog/service/node_modules/egg/lib/loader/agent_worker_loader.js:15:10)

at new EggApplication (/Users/zhangbing/github/CodeTest/react_blog/service/node_modules/egg/lib/egg.js:54:17)

at new Agent (/Users/zhangbing/github/CodeTest/react_blog/service/node_modules/egg/lib/agent.js:22:5)

at Object. (/Users/zhangbing/github/CodeTest/react_blog/service/node_modules/egg-cluster/lib/agent_worker.js:28:15)

at Module._compile (internal/modules/cjs/loader.js:778:30)

at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)

at Module.load (internal/modules/cjs/loader.js:653:32)

2020-02-27 22:51:20,141 ERROR 3754 nodejs.AgentWorkerDiedError: [master] agent_worker#1:3759 died (code: 1, signal: null)

at Master.onAgentExit (/Users/zhangbing/github/CodeTest/react_blog/service/node_modules/egg-cluster/lib/master.js:425:17)

at Master.emit (events.js:198:13)

at Messenger.sendToMaster (/Users/zhangbing/github/CodeTest/react_blog/service/node_modules/egg-cluster/lib/utils/messenger.js:137:17)

at Messenger.send (/Users/zhangbing/github/CodeTest/react_blog/service/node_modules/egg-cluster/lib/utils/messenger.js:102:12)

at ChildProcess.agentWorker.once (/Users/zhangbing/github/CodeTest/react_blog/service/node_modules/egg-cluster/lib/master.js:289:22)

at Object.onceWrapper (events.js:286:20)

at ChildProcess.emit (events.js:198:13)

at Process.ChildProcess._handle.onexit (internal/child_process.js:248:12)

name: "AgentWorkerDiedError"

pid: 3754

hostname: ibing.local

2020-02-27 22:51:20,141 ERROR 3754 [master] agent_worker#1:3759 start fail, exiting with code:1

2020-02-27 22:51:20,142 ERROR 3754 [master] exit with code:1

{ Error: /Users/zhangbing/github/CodeTest/react_blog/service/node_modules/egg-bin/lib/start-cluster {"typescript":false,"declarations":true,"workers":1,"baseDir":"/Users/zhangbing/github/CodeTest/react_blog/service","framework":"/Users/zhangbing/github/CodeTest/react_blog/service/node_modules/egg"} exit with code 1

at ChildProcess.proc.once.code (/Users/zhangbing/github/CodeTest/react_blog/service/node_modules/common-bin/lib/helper.js:56:21)

at Object.onceWrapper (events.js:286:20)

at ChildProcess.emit (events.js:198:13)

at Process.ChildProcess._handle.onexit (internal/child_process.js:248:12) code: 1 }

npm ERR! code ELIFECYCLE

npm ERR! errno 1

npm ERR! service@1.0.0 dev: `egg-bin dev`

npm ERR! Exit status 1

npm ERR!

npm ERR! Failed at the service@1.0.0 dev script.

npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

问题出现的环境背景及自己尝试过哪些方法

环境信息:

OS:Mac OS X 10.15.3

Egg.js 2.15.1

egg-mysql 3.0.0

MySQL连接信息无误,本地Navicat可以正常操作,搜索了一圈依然没解决,Egg.js官方issues有提到,但貌似没有解决方案

相关代码

config/config.default.js

/* eslint valid-jsdoc: "off" */

'use strict';

/**

* @param {Egg.EggAppInfo} appInfo app info

*/

module.exports = appInfo => {

/**

* built-in config

* @type {Egg.EggAppConfig}

**/

const config = exports = {};

// use for cookie sign key, should change to your own and keep security

config.keys = appInfo.name + '_1582792561373_6314';

// add your middleware config here

config.middleware = [];

// add your user config here

const userConfig = {

// myAppName: 'egg',

};

config.mysql = {

// 单数据库信息配置

client: {

// host

host: 'localhost',

// 端口号

port: '3306',

// 用户名

user: 'root',

// 密码

password: '123321',

// 数据库名

database: 'react-blog',

},

// 是否加载到 app 上,默认开启

app: true,

// 是否加载到 agent 上,默认关闭

agent: false,

};

return {

...config,

...userConfig,

};

};

config/plugin.js

module.exports = {

enable: true,

package: 'egg-mysql',

};

Egg.js 是一个基于 Node.js 和 Koa 的 Web 框架,它提供了很多便捷的功能和插件,让开发者可以快速地构建 Web 应用程序。TypeScript 则是一种静态类型检查的编程语言,可以帮助我们在编写代码时更早地捕获一些错误,提高代码的可维护性和可读性。MySQL 则是一种常用的关系型数据库,用于存储和管理数据。 在 Egg.js 中使用 TypeScriptMySQL,我们可以使用 egg-mysql 插件来连接和操作 MySQL 数据库,并使用 TypeScript 的类型检查来保证代码的正确性。具体步骤如下: 1. 创建 Egg.js 项目 ```bash $ npm i egg-init -g $ egg-init egg-typescript-mysql --type=ts $ cd egg-typescript-mysql $ npm i ``` 2. 安装 egg-mysql 插件 ```bash $ npm i egg-mysql ``` 3. 配置 MySQL 数据库连接 在 `config/config.default.ts` 中添加以下配置: ```typescript config.mysql = { client: { host: 'localhost', port: '3306', user: 'root', password: 'password', database: 'test', }, app: true, agent: false, }; ``` 其中,`host`、`port`、`user`、`password`、`database` 分别为你的 MySQL 数据库连接信息。 4. 创建 Model 在 `app/model` 目录下创建一个新的文件 `user.ts`,用于定义操作用户数据的 Model: ```typescript import { Application } from 'egg'; export default (app: Application) => { const { INTEGER, STRING } = app.Sequelize; const User = app.model.define('user', { id: { type: INTEGER, primaryKey: true, autoIncrement: true, }, name: { type: STRING(50), allowNull: false, }, age: { type: INTEGER, allowNull: false, }, }); return User; }; ``` 其中,`app.Sequelize` 是通过 egg-sequelize 插件提供的 Sequelize 实例。 5. 创建 Controller 和 Service 在 `app/controller` 目录下创建一个新的文件 `user.ts`,用于处理用户相关的请求: ```typescript import { Controller } from 'egg'; export default class UserController extends Controller { async index() { const { ctx } = this; const users = await ctx.service.user.list(); ctx.body = { users }; } async show() { const { ctx } = this; const user = await ctx.service.user.get(ctx.params.id); ctx.body = { user }; } async create() { const { ctx } = this; const { name, age } = ctx.request.body; const user = await ctx.service.user.create(name, age); ctx.body = { user }; } async update() { const { ctx } = this; const { id, name, age } = ctx.request.body; const user = await ctx.service.user.update(id, name, age); ctx.body = { user }; } async destroy() { const { ctx } = this; const user = await ctx.service.user.delete(ctx.params.id); ctx.body = { user }; } } ``` 在 `app/service` 目录下创建一个新的文件 `user.ts`,用于实现对用户数据的操作: ```typescript import { Service } from 'egg'; export default class UserService extends Service { async list() { const { ctx } = this; const users = await ctx.model.User.findAll(); return users; } async get(id: number) { const { ctx } = this; const user = await ctx.model.User.findByPk(id); return user; } async create(name: string, age: number) { const { ctx } = this; const user = await ctx.model.User.create({ name, age }); return user; } async update(id: number, name: string, age: number) { const { ctx } = this; const user = await ctx.model.User.findByPk(id); if (!user) { ctx.throw(404, 'user not found'); } await user.update({ name, age }); return user; } async delete(id: number) { const { ctx } = this; const user = await ctx.model.User.findByPk(id); if (!user) { ctx.throw(404, 'user not found'); } await user.destroy(); return user; } } ``` 6. 路由配置 在 `app/router.ts` 中配置路由: ```typescript import { Application } from 'egg'; export default (app: Application) => { const { controller, router } = app; router.get('/users', controller.user.index); router.get('/users/:id', controller.user.show); router.post('/users', controller.user.create); router.put('/users', controller.user.update); router.delete('/users/:id', controller.user.destroy); }; ``` 7. 启动应用 ```bash $ npm run dev ``` 以上就是使用 Egg.jsTypeScriptMySQL 开发 Web 应用程序的基本步骤。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值