【问题标题】:Sequelize Error with query sending Error: read ECONNRESETSequelize Error with query sent Error: read ECONNRESET
【发布时间】:2017-09-04 03:22:31
【问题描述】:

PostgreSQL 9.6.2 我从 node.js / express.js 应用程序发送查询。

sequelize 提供doc how to make the query,本例为findAll。

我尝试做同样的例子。

console.log('user id: ', req.decoded);
db.orders.findAll({where: {userId: req.decoded.id}})
    .then(function (orders) {
        console.log('orders from db: ', orders);
    })
    .catch(function (err) {
        console.log('orders request error from db: ', err);
    });
console.log('end of function');

控制台日志:

user id:  { id: 2 }
end of function
orders request error from db:  { SequelizeConnectionError: read ECONNRESET
    at D:\NodeJSProjects\ParkingHouse\node_modules\sequelize\lib\dialects\postgres\connection-manager.js:110:20
    at Connection.<anonymous> (D:\NodeJSProjects\ParkingHouse\node_modules\pg\lib\client.js:186:5)
    at emitOne (events.js:96:13)
    at Connection.emit (events.js:188:7)
    at Socket.<anonymous> (D:\NodeJSProjects\ParkingHouse\node_modules\pg\lib\connection.js:86:10)
    at emitOne (events.js:96:13)
    at Socket.emit (events.js:188:7)
    at emitErrorNT (net.js:1278:8)
    at _combinedTickCallback (internal/process/next_tick.js:74:11)
    at process._tickCallback (internal/process/next_tick.js:98:9)
  name: 'SequelizeConnectionError',
  message: 'read ECONNRESET',
  parent: 
   { Error: read ECONNRESET
       at exports._errnoException (util.js:1022:11)
       at TCP.onread (net.js:569:26) code: 'ECONNRESET', errno: 'ECONNRESET', syscall: 'read' },
  original: 
   { Error: read ECONNRESET
       at exports._errnoException (util.js:1022:11)
       at TCP.onread (net.js:569:26) code: 'ECONNRESET', errno: 'ECONNRESET', syscall: 'read' } }

一段时间后,请求被重复,然后我从 db 获取数据。 我找到了类似的主题Node js ECONNRESET,但我没有找到答案。

  1. db 为什么要关闭连接?以及如何修复它。

【问题讨论】:

    标签: node.js postgresql nodes sequelize.js


    【解决方案1】:

    您正在失去联系。可能您没有在框架中调用 next() 。或者不关闭交易。 这种结构没有意义:

    .then(function (foundUser) {
          return foundUser;
      }).catch(function (err) {
        //get here the exception
        return err;
      });
    

    只需删除它。然后将您的控制器重写为类似

    const user = await authorize(req);
    if (!user) {
        res.status(401).send('Authorization required');
        return next();
    }
    res.status(200).send('All ok');
    next();
    

    出现错误时,您的框架应自动为您提供 500。如果您想手动处理它,请使用 try { await Promise } catch (err) { } 如果你使用还不支持 await/async 的东西,请查看http://koajs.com/

    【讨论】:

    • 以后我想把更多的逻辑放到authorize.js中,例如检查地理位置,ip等。我想找到用户,完成逻辑操作,然后将用户返回给控制器。所以我希望在 authorize.js 中期待承诺并与用户合作。
    【解决方案2】:

    问题出在防病毒 F-Secure 中,更改它并一切正常。 一些问题: https://community.f-secure.com/t5/Business/DeepGuard-seems-to-cause/td-p/88447

    【讨论】:

      【解决方案3】:

      我今天遇到了类似的错误

      { 
       error: {
         Error: write ECONNRESET at WriteWrap.afterWrite (net.js:788:14) errno: 'ECONNRESET',
         code: 'ECONNRESET', syscall: 'write'
       } 
      }
      

      需要设置数据库最大池或者避免同时发送多个。

      这里有一个github 线程可以帮助你。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-11-10
        • 1970-01-01
        • 2022-12-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-09-18
        • 2021-12-22
        相关资源
        最近更新 更多