2017-04-08 288 views
0

PostgreSQL 9.6.2我从node.js/express.js应用程序发送查询。发送查询时出现Sequelize错误错误:读取ECONNRESET

该续集提供了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' } } 

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

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

回答

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。如果您想手动处理,请尝试{await Promise} catch(err){} 如果您使用的某些内容不支持await/async,请查看http://koajs.com/

+0

未来我想把更多的逻辑放到authorize.js中,例如检查地理位置,ip等。我想找到用户,完成逻辑操作,然后将用户返回给控制器。所以我想期待承诺,并在authorize.js中与用户一起工作。 –