我有一个用nodejs编写的应用程序,用于后端和sequelize连接到mysql。当我在中运行我的应用程序并向端点发送请求时,一切都很好。但问题出在测试模式下。当我试图用supertest测试我的端点时,我得到了一个非常烦人的错误,上面写着:[SequelizeConnectionError]: authSwitch.authSwitchRequest is not a function
完整的错误消息是:
Attempted to log "mysql could not connect! ConnectionError [SequelizeConnectionError]: authSwitch.authSwitchRequest is not a function
at ConnectionManager.connect (D:ProgrammingNodejsFamiShopnode_modulessequelizelibdialectsmysqlconnection-manager.js:126:17)
at processTicksAndRejections (node:internal/process/task_queues:96:5)
at ConnectionManager._connect (D:ProgrammingNodejsFamiShopnode_modulessequelizelibdialectsabstractconnection-manager.js:318:24)
at D:ProgrammingNodejsFamiShopnode_modulessequelizelibdialectsabstractconnection-manager.js:250:32
at ConnectionManager.getConnection (D:ProgrammingNodejsFamiShopnode_modulessequelizelibdialectsabstractconnection-manager.js:280:7)
at D:ProgrammingNodejsFamiShopnode_modulessequelizelibsequelize.js:613:26
at Sequelize.authenticate (D:ProgrammingNodejsFamiShopnode_modulessequelizelibsequelize.js:867:5) {
parent: TypeError: authSwitch.authSwitchRequest is not a function
at ClientHandshake.handshakeResult (D:ProgrammingNodejsFamiShopnode_modulesmysql2libcommandsclient_handshake.js:150:22)
at ClientHandshake.execute (D:ProgrammingNodejsFamiShopnode_modulesmysql2libcommandscommand.js:39:22)
at Connection.handlePacket (D:ProgrammingNodejsFamiShopnode_modulesmysql2libconnection.js:425:32)
at PacketParser.onPacket (D:ProgrammingNodejsFamiShopnode_modulesmysql2libconnection.js:75:12)
at PacketParser.executeStart (D:ProgrammingNodejsFamiShopnode_modulesmysql2libpacket_parser.js:75:16)
at Socket.<anonymous> (D:ProgrammingNodejsFamiShopnode_modulesmysql2libconnection.js:82:25)
at Socket.emit (node:events:394:28)
at addChunk (node:internal/streams/readable:312:12)
at readableAddChunk (node:internal/streams/readable:287:9)
at Socket.Readable.push (node:internal/streams/readable:226:10),
original: TypeError: authSwitch.authSwitchRequest is not a function
at ClientHandshake.handshakeResult (D:ProgrammingNodejsFamiShopnode_modulesmysql2libcommandsclient_handshake.js:150:22)
at ClientHandshake.execute (D:ProgrammingNodejsFamiShopnode_modulesmysql2libcommandscommand.js:39:22)
at Connection.handlePacket (D:ProgrammingNodejsFamiShopnode_modulesmysql2libconnection.js:425:32)
at PacketParser.onPacket (D:ProgrammingNodejsFamiShopnode_modulesmysql2libconnection.js:75:12)
at PacketParser.executeStart (D:ProgrammingNodejsFamiShopnode_modulesmysql2libpacket_parser.js:75:16)
at Socket.<anonymous> (D:ProgrammingNodejsFamiShopnode_modulesmysql2libconnection.js:82:25)
at Socket.emit (node:events:394:28)
at addChunk (node:internal/streams/readable:312:12)
at readableAddChunk (node:internal/streams/readable:287:9)
at Socket.Readable.push (node:internal/streams/readable:226:10)
}". ```
I searched the internet but I could not find a solution for this.
can you help me?
我在设置测试套件时也遇到了这个问题。
问题是我在创建数据库连接时忘记了使用wait。我有这样的东西:
connection.client = new Sequelize(connectionString('ems'), {
dialect: 'mysql',
.....
})
connection.client.authenticate()
我把它改成这样:
connection.client = new Sequelize(connectionString('ems'), {
dialect: 'mysql',
.....
})
await connection.client.authenticate() // await this
成功了。
就在修复后,我遇到了另一个烦恼:测试运行完成后一秒钟,Jest没有退出通过在测试文件中添加以下行修复了此问题。
afterAll(() => connection.client.close())
嘿,首先你需要安装类似npm install sequelize sequelize-cli
的sequelize命令
并运行此命令sequelize init
来运行此命令,它将在后端文件夹中创建一些文件,如config、模型迁移、种子程序
#然后在配置中,你会看到这样的连接代码:
{
"development": {
"username": "root",
"password": "*****",
"database": "your project name in the database",
"host": "localhost",
"dialect": "mysql"
},
"test": {
"username": "root",
"password": null,
"database": "database_test",
"host": "127.0.0.1",
"dialect": "mysql"
},
"production": {
"username": "root",
"password": null,
"database": "database_production",
"host": "127.0.0.1",
"dialect": "mysql"
}
}
然后运行后端,它将创建您在模型文件中提供的所有表。