Sequelize Error with query sending Error: read ECONNRESET

限于喜欢 提交于 2019-12-12 03:45:07

问题


PostgreSQL 9.6.2 I send query from node.js / express.js application.

The sequelize provide the doc how to make the query, this example findAll.

I try to make same example.

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');

Console log:

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' } }

After some time the request is repeated and then I get the data from db. I found the similar topic Node js ECONNRESET but I don't found answer.

  1. Why db close the connection? And how to repair it.

回答1:


The problem was in antivirus F-Secure, change it and all works. Some problem: https://community.f-secure.com/t5/Business/DeepGuard-seems-to-cause/td-p/88447




回答2:


You are loosing you connection. Probably you are not calling next() in your framework. Or do not close transaction. This structure does not make sense:

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

Just remove it. Then rewrite your controller to something like

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

On error your framework should serve 500 automatically for you. If you want to handle it manually use try { await Promise } catch (err) { } If you use something that does not support await/async yet, than take a look at http://koajs.com/




回答3:


I had similar Error today

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

You need to set the database maximum pool or avoid sending multiple at the same time.

Here there is a github thread that may help you.



来源:https://stackoverflow.com/questions/43300185/sequelize-error-with-query-sending-error-read-econnreset

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!