【问题标题】:Write ECONNRESET error写入 ECONNRESET 错误
【发布时间】:2018-07-11 22:40:28
【问题描述】:

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

当我调用 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"
  }
}

【问题讨论】:

  • 你能发布完整的错误日志吗?
  • 你是通过 npm start 运行它吗?如果是,请也分享 package.json
  • 你在哪里定义query 行:query.on(... ?我没有看到它被定义,你的错误可能会因为不在范围内而吞下一个简单的错误。
  • 好的,但client 和其他可能的情况相同。您是否发布了所有相关代码?
  • 您是否尝试运行简单的 sql 命令,例如 SELECT 1,以查看您是否真的能够从节点连接到 postgres?我假设你的错误发生在这一行client.connect('error', function (err, data) {

标签: node.js


【解决方案1】:

您似乎没有遵循正确的 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. 
});

【讨论】:

  • 哦是的,就是这样!谢谢大家。我对node.js有点新,所以我还在学习抱歉:/
猜你喜欢
  • 2020-06-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-09-28
  • 2014-01-24
  • 1970-01-01
  • 2018-06-08
  • 1970-01-01
相关资源
最近更新 更多