Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

关于mysql数据使用的问题 #16

Closed
slgluo opened this issue Jan 12, 2020 · 17 comments
Closed

关于mysql数据使用的问题 #16

slgluo opened this issue Jan 12, 2020 · 17 comments
Labels
needs repro 无法复现

Comments

@slgluo
Copy link

slgluo commented Jan 12, 2020

Describe the bug
我配置好mysql数据库之后,收到消息时,出现了以下问题:

(node:4164) UnhandledPromiseRejectionWarning: TypeError: Cannot read property '_timestamp' of undefined
    at MysqlDatabase.getGroup (F:\WebProject\tool-man\node_modules\koishi-database-mysql\dist\group.js:14:44)
    at MysqlDatabase.observeGroup (F:\WebProject\tool-man\node_modules\koishi-database-mysql\dist\group.js:61:37)
    at Array.App._preprocess (F:\WebProject\tool-man\node_modules\koishi-core\dist\app.js:134:55)
    at next (F:\WebProject\tool-man\node_modules\koishi-core\dist\app.js:199:103)
    at EventEmitter.App._applyMiddlewares (F:\WebProject\tool-man\node_modules\koishi-core\dist\app.js:206:19)
    at EventEmitter.emit (events.js:200:13)
    at App.emitEvent (F:\WebProject\tool-man\node_modules\koishi-core\dist\app.js:348:30)
    at HttpServer.dispatchMeta (F:\WebProject\tool-man\node_modules\koishi-core\dist\server.js:162:17)
    at IncomingMessage.<anonymous> (F:\WebProject\tool-man\node_modules\koishi-core\dist\server.js:249:22)
    at IncomingMessage.emit (events.js:205:15)
(node:4164) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 5)

下面是我的配置:

const app = new App({
    type: 'http',
    port: 8080,
    server: 'http://localhost:5700',
    database: {
        mysql: {
            host: '*.*.*.*',
            port: 3306,
            user: 'root',
            password: '******',
            database: 'coolq'
        },
    }
})

databases

Additional context
此外,看了一下你之前关于mysql初始化的问题,是不是要手动创建那些你在API里调用的表和字段?

@shigma
Copy link
Member

shigma commented Jan 12, 2020

首先感谢你的持续支持!mua

这应该确实是 koishi-database-mysql 的 bug,我想下怎么弄,稍后给你回复。

初始化那个 issue 我本来计划自动检查数据库的表和字段然后自动补充的,但是想到 js 中的类型和 sql 中的类型没法做一一对应,所以放弃了这个想法。

@shigma shigma added this to the 1.3 milestone Jan 12, 2020
@shigma
Copy link
Member

shigma commented Jan 12, 2020

刚刚发布了 koishi-database-mysql 1.0.4 版本,你可以检查一下是否能解决你的问题。

@slgluo
Copy link
Author

slgluo commented Jan 12, 2020

刚刚发布了 koishi-database-mysql 1.0.4 版本,你可以检查一下是否能解决你的问题。

那个异常没有出现了,但是抛了两个的新的异常:

  1. 这个是我在connect之后,调用app.database.getUser(app.selefId)出现的问题:
(node:7132) UnhandledPromiseRejectionWarning: TypeError: app.database.getUser is not a function
    at EventEmitter.<anonymous> (F:\WebProject\tool-man\index.js:34:35)
    at EventEmitter.emit (events.js:200:13)
    at App.start (F:\WebProject\tool-man\node_modules\koishi-core\dist\app.js:319:23)
    at processTicksAndRejections (internal/process/task_queues.js:89:5)
(node:7132) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:7132) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
  1. 这个是收到消息时抛出的异常:
(node:7132) UnhandledPromiseRejectionWarning: TypeError: this.database.observeGroup is not a function
    at Array.App._preprocess (F:\WebProject\tool-man\node_modules\koishi-core\dist\app.js:134:55)
    at next (F:\WebProject\tool-man\node_modules\koishi-core\dist\app.js:199:103)
    at EventEmitter.App._applyMiddlewares (F:\WebProject\tool-man\node_modules\koishi-core\dist\app.js:206:19)
    at EventEmitter.emit (events.js:200:13)
    at App.emitEvent (F:\WebProject\tool-man\node_modules\koishi-core\dist\app.js:348:30)
    at HttpServer.dispatchMeta (F:\WebProject\tool-man\node_modules\koishi-core\dist\server.js:162:17)
    at IncomingMessage.<anonymous> (F:\WebProject\tool-man\node_modules\koishi-core\dist\server.js:249:22)
    at IncomingMessage.emit (events.js:205:15)
    at endReadableNT (_stream_readable.js:1137:12)
    at processTicksAndRejections (internal/process/task_queues.js:84:9)
(node:7132) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 3)

你这样好调试吗,方便的话我可以把工程和一些必要的信息发给你

@shigma
Copy link
Member

shigma commented Jan 12, 2020

我这现在可以正常使用数据库,无法复现你的问题。你额外提供一些信息吧,比如 index.js 的内容。

@slgluo
Copy link
Author

slgluo commented Jan 12, 2020

我这现在可以正常使用数据库,无法复现你的问题。你额外提供一些信息吧,比如 index.js 的内容。

以下是index.js的所有内容:

const { App } = require('koishi')
require('koishi-database-mysql')
const lxPlugin = require('./plugin/liuxing')

const lxOptions = {
    watchUser: '';,
    watchGroup: '',
    lxGroups: [ ]
}

const app = new App({
    type: 'http',
    port: 8080,
    server: 'http://localhost:5700',
    database: {
        mysql: {
            host: '',
            port: 3306,
            user: 'root',
            password: '',
            database: 'coolq'
        },
    }
})

app.user(975394636).command('状态')
    .action(({ meta }) => meta.$send('正常'));

app.receiver.on('connect', async () => {
    // 获取用户数据
    let user = await app.database.getUser(app.selfId)
    console.log("user:", user)
})

app.plugin(lxPlugin, lxOptions)

app.start()

其他的代码没有涉及到数据库,lxPlugin这个插件是接收数据做一些处理,然后转发,其他没了

@shigma
Copy link
Member

shigma commented Jan 13, 2020

@slgluo 我这还是没法复现,你那 koishi 是 1.2.0 版本吗?

@slgluo
Copy link
Author

slgluo commented Jan 13, 2020

@slgluo 我这还是没法复现,你那 koishi 是 1.2.0 版本吗?

之前是v1.1.1的,然后我升级到1.2.0,还是还出现同样的问题

@slgluo
Copy link
Author

slgluo commented Jan 13, 2020

@shigma
我在调试过程中发现,database这个属性就是一个空对象

  1. new App()时,在构造函数中:
 this.options = { ...defaultOptions, ...options };
 if (options.database && Object.keys(options.database).length) {
       this.database = database_1.createDatabase(options.database);
 }
  1. database_1.createDatabase()方法
function createDatabase(config) {
    return new DatabaseManager(config).database;
}
  1. new DatabaseManager()时,构造函数中:
    constructor(config) {
        this.config = config;
        this.database = {};
        this.implicitTables = {};
        // 我没有配置$tables属性,这个for方法不走
        this.explicitTables = config.$tables || {};
        for (const table in this.explicitTables) {
            const name = this.explicitTables[table];
            if (!config[name])
                throw new Error(`database "${name}" not configurated`);
        }
       // subdatabases也为空,不走
        for (const type in subdatabases) {
            this.bindSubdatabase(type, config[type]);
        }
    }

以上,this.database = database_1.createDatabase(options.database),这个最终为{}空对象,所以app.database为{}
所以,这个要怎么配置呢,还是我配置错了

@shigma
Copy link
Member

shigma commented Jan 13, 2020

@slgluo 非常感谢你的帮助!subdatabases 为空说明 mysql 没有成功安装。你可以检查一下 koishi-database-mysql 是不是有其他的 koishi-core 作为依赖(简单来说就是看一下 koishi-database-mysql 的 node_modules 有没有 koishi-core)。Koishi 的数据库会采用注入的方式,因此如果 koishi-database-mysql 依赖的 koishi-core 不是你使用的,那么就会注入到错误的数据库中。

@shigma shigma closed this as completed in 3ce0c70 Jan 13, 2020
@shigma shigma reopened this Jan 13, 2020
@slgluo
Copy link
Author

slgluo commented Jan 14, 2020

@slgluo 非常感谢你的帮助!subdatabases 为空说明 mysql 没有成功安装。你可以检查一下 koishi-database-mysql 是不是有其他的 koishi-core 作为依赖(简单来说就是看一下 koishi-database-mysql 的 node_modules 有没有 koishi-core)。Koishi 的数据库会采用注入的方式,因此如果 koishi-database-mysql 依赖的 koishi-core 不是你使用的,那么就会注入到错误的数据库中。

koishi-database-mysqlkoishi-core依赖,我重新安装依赖之后,解决了上面的问题。但是又出现以下问题:

(node:5236) UnhandledPromiseRejectionWarning: Error: Handshake inactivity timeout
    at Handshake.<anonymous> (F:\WebProject\tool-man\node_modules\mysql\lib\protocol\Protocol.js:160:17)
    at Handshake.emit (events.js:200:13)
    at Handshake._onTimeout (F:\WebProject\tool-man\node_modules\mysql\lib\protocol\sequences\Sequence.js:124:8)
    at Timer._onTimeout (F:\WebProject\tool-man\node_modules\mysql\lib\protocol\Timer.js:32:23)
    at listOnTimeout (internal/timers.js:531:17)
    at processTimers (internal/timers.js:475:7)
    --------------------
    at Protocol._enqueue (F:\WebProject\tool-man\node_modules\mysql\lib\protocol\Protocol.js:144:48)
    at Protocol.handshake (F:\WebProject\tool-man\node_modules\mysql\lib\protocol\Protocol.js:51:23)
    at PoolConnection.connect (F:\WebProject\tool-man\node_modules\mysql\lib\Connection.js:119:18)
    at Pool.getConnection (F:\WebProject\tool-man\node_modules\mysql\lib\Pool.js:48:16)
    at Pool.query (F:\WebProject\tool-man\node_modules\mysql\lib\Pool.js:202:8)
    at F:\WebProject\tool-man\node_modules\koishi-database-mysql\dist\database.js:51:27
    at new Promise (<anonymous>)
    at MysqlDatabase.query (F:\WebProject\tool-man\node_modules\koishi-database-mysql\dist\database.js:50:20)
    at MysqlDatabase.select (F:\WebProject\tool-man\node_modules\koishi-database-mysql\dist\database.js:62:25)
    at MysqlDatabase.getGroup (F:\WebProject\tool-man\node_modules\koishi-database-mysql\dist\group.js:17:35)
(node:5236) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 13)

o(╯□╰)o

@shigma
Copy link
Member

shigma commented Jan 14, 2020

@slgluo 这个是连接都出错了,你尝试着重新运行。同时问一下你的 mysql 版本(我测试用的是 5.x)?

@slgluo
Copy link
Author

slgluo commented Jan 14, 2020

@slgluo 这个是连接都出错了,你尝试着重新运行。同时问一下你的 mysql 版本(我测试用的是 5.x)?

mysql 5.7.28,我连接的远程的mysql,用nvacat可以远程连接,应该没问题,可以连接。我再确认一下,是koishi配置好之后,mysql什么都不用动吗,不需要手动创建数据库和表是吧?

@shigma
Copy link
Member

shigma commented Jan 14, 2020

@slgluo 那应该不是这方面的问题,我也是远程连接成功的。Koishi 配置好后,MySQL 是要创建数据库和表的。数据库应该与你 mysql 的配置相符合,表有两个 user 和 group,文档中有它们的细节。如果想详聊的话可以给我邮件,我们交换一下社交账号。

@slgluo
Copy link
Author

slgluo commented Jan 14, 2020

@slgluo 那应该不是这方面的问题,我也是远程连接成功的。Koishi 配置好后,MySQL 是要创建数据库和表的。数据库应该与你 mysql 的配置相符合,表有两个 user 和 group,文档中有它们的细节。如果想详聊的话可以给我邮件,我们交换一下社交账号。

slgluo@qq.com

@shigma shigma added the needs repro 无法复现 label Jan 22, 2020
@shigma shigma removed this from the 1.3 milestone Jan 22, 2020
@undefined-moe
Copy link
Member

数据库初始化时应当自动检查数据表并创建,而非手动处理

@shigma
Copy link
Member

shigma commented Mar 26, 2020

数据库初始化时应当自动检查数据表并创建,而非手动处理

@masnn 你说的对,后面应该会支持的。顺便这可能是另一个 issue 的事情了(

@shigma shigma removed the bug BUG label Apr 8, 2020
@shigma
Copy link
Member

shigma commented Apr 18, 2020

这个 issue 已经长期没有更新,在此期间 koishi 也更新了很多版本,上面的问题很可能已经无法复现了,因此我会把它关掉。如果还有问题可以另开一个 issue。

@shigma shigma closed this as completed Apr 18, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs repro 无法复现
Projects
None yet
Development

No branches or pull requests

3 participants