写入ECONNRESET错误

时间:2018-07-11 14:09:18

标签: node.js

我看了一些可能解决我问题的答案,但是找不到解决方法。

当我致电localhost:3000 / users / create_account时,出现错误ECONNRESET。 它涉及以下服务:

router.post('/create_account', function (req, res, next) {
const results = [];
client.connect();

// Grab data from http request
const data = {
username: req.body.username, name: req.body.user,
firstname: req.body.firstname, email: req.body.email,
location: req.body.location
};

// Get a Postgres client from the connection pool
client.connect('error', function (err, data) {
// Handle connection errors
if (err) {
  done();
  console.log(err);
  return res.status(500).json({ success: false, data: err });
}

const queryInsert = 'INSERT INTO Users(username, name,'
  + 'firstname, email, location) values($1, $2, $3, $4, $5)';
const values = [data.username, data.name,
data.firstname, data.email, data.location];

// SQL Query > Insert Data into Users 
client.query(queryInsert, values, (err, res) => {
  if (err) {
    console.log(err.stack)
  } else {
    query.on('end', () => {
      done();

      Promise.all([queryInsert])
        .then(() => client.end()).catch(err => console.log(err));
      return res.json(results);
    });
  };
});

请问有人知道导致该问题的原因以及如何解决此问题吗?

这是完整的错误:

Error: write ECONNRESET
at _errnoException (util.js:992:11)
at Socket._writeGeneric (net.js:764:25)
at Socket._write (net.js:783:8)
at doWrite (_stream_writable.js:397:12)
at writeOrBuffer (_stream_writable.js:383:5)
at Socket.Writable.write (_stream_writable.js:290:11)
at Socket.write (net.js:707:40)
at Connection._send (C:\Users\Amaris\Desktop\NodeJS_Projects\basefugees-dev-backend\node_modules\pg\lib\connection.js:198:24)
at Connection.password (C:\Users\Amaris\Desktop\NodeJS_Projects\basefugees-dev-backend\node_modules\pg\lib\connection.js:190:8)
at C:\Users\Amaris\Desktop\NodeJS_Projects\basefugees-dev-backend\node_modules\pg\lib\client.js:98:9
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! basefugees-dev-backend@0.0.1 start: `node ./bin/www node`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the basefugees-dev-backend@0.0.1 start script.
npm ERR! This is probably not a problem with npm. There is likely additional 
logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     C:\Users\Amaris\AppData\Roaming\npm-cache\_logs\2018-07- 11T14_05_26_893Z-debug.log

还有我的package.json:

{
  "name": "basefugees-dev-backend",
  "version": "0.0.1",
  "private": true,
  "scripts": {
    "start": "node ./bin/www node",
    "database": "node ./models/database",
    "test": "mocha"
  },
  "dependencies": {
    "body-parser": "^1.18.3",
    "cookie-parser": "~1.4.3",
    "debug": "~2.6.9",
    "dotenv": "^6.0.0",
    "express": "~4.16.0",
    "http-errors": "~1.6.2",
    "morgan": "~1.9.0",
    "pg": "^6.1.0",
    "pg-hstore": "^2.3.2"
  },
  "devDependencies": {
    "chai": "^4.1.2",
    "mocha": "^5.2.0"
  }
}

1 个答案:

答案 0 :(得分:2)

您似乎没有遵循正确的API进行错误处理。从docs开始,没有client.connect()重载,该重载将字符串作为第一个参数。可能正在尝试根据错误堆栈将该值用作密码或其他配置值。

您要使用的是:

client.on('error', (err) => { 
   // do whatever.  Though again you're calling a `done` that doesn't seem defined, and some other concerns. 
});