0

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.
Sergei R
  • 701
  • 4
  • 10
  • 24

3 Answers3

1

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/

Sergey Yarotskiy
  • 4,536
  • 2
  • 19
  • 27
  • In future I want to place the more logics into authorize.js, for example check geolocation, ip, etc. I want to find user, compleate logic operation and then return User to controller. So I want to expect the promise and work with user in authorize.js. – Sergei R Apr 08 '17 at 22:01
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

Sergei R
  • 701
  • 4
  • 10
  • 24
0

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.

Niyongabo Eric
  • 1,333
  • 18
  • 21