NodeJS 运行npm运行测试时,序列化连接拒绝错误:连接ECONNREFUSED 127.0.0.1:3306我收到错误

sqxo8psd  于 2023-02-18  发布在  Node.js
关注(0)|答案(1)|浏览(183)

这是我的config.json文件:

{
  "development": {
    "username": "xyz",
    "password": "password123",
    "database": "db1",
    "host": "localhost",
    "dialect": "postgres"
  },
  "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"
  }
}

这是我的app.js文件

const useit = async () => {
    await sequelize.authenticate();
}
//Listening 
app.listen({ port: 8000 }, async () => {
  console.log('Server up on http://localhost:8000')
  useit();
  console.log('Database Connected!')
  await sequelize.sync({alter:true});
  
})

module.exports = app;

在ORM中,我使用Sequalize沿着postgres作为我的后端。一切都很好,我所有的REST API端点都很好,但是,当我运行npm run test时,我收到了以下错误:

console.warn
    failed to find WebSocket

      at $.send (eval at oo_cm (app.js:386:57), <anonymous>:1:5120)

  console.warn
    failed to find WebSocket

      at $.send (eval at oo_cm (app.js:386:57), <anonymous>:1:5120)

 FAIL  test/app.test.js
  ✕ GET random not found endpoint (33 ms)

  ● GET random not found endpoint

    SequelizeConnectionRefusedError: connect ECONNREFUSED 127.0.0.1:3306

      at Client._connectionCallback (node_modules/sequelize/src/dialects/postgres/connection-manager.js:179:24)
      at Client._handleErrorWhileConnecting (node_modules/pg/lib/client.js:316:19)
      at Client._handleErrorEvent (node_modules/pg/lib/client.js:326:19)
      at Socket.reportStreamError (node_modules/pg/lib/connection.js:57:12)

  ● GET random not found endpoint

    SequelizeConnectionRefusedError: connect ECONNREFUSED 127.0.0.1:3306

      at Client._connectionCallback (node_modules/sequelize/src/dialects/postgres/connection-manager.js:179:24)
      at Client._handleErrorWhileConnecting (node_modules/pg/lib/client.js:316:19)
      at Client._handleErrorEvent (node_modules/pg/lib/client.js:326:19)
      at Socket.reportStreamError (node_modules/pg/lib/connection.js:57:12)

我不知道整个代码的问题是什么。试图改变配置文件和app.js里面的东西,但没有工作。我的测试用例总是失败与相同的错误。请帮助我。

mccptt67

mccptt671#

3306是MySQL的端口号。但是,您似乎是使用PostgreSQL驱动程序连接的。

  • 检查您的数据库连接配置。运行MySQL或PostgreSQL,并检查配置是否正确。
  • 您是否使用了配置文件中的正确配置?另外,是否正确设置了环境变量NODE_ENV?(开发)
  • DB服务是否正在运行?

相关问题