首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >编写ECONNRESET错误

编写ECONNRESET错误
EN

Stack Overflow用户
提问于 2018-07-11 14:09:17
回答 1查看 1.6K关注 0票数 0

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

当我调用localhost:3000/user/create_account时,会出现一个错误ECONNRESET。它涉及这项服务:

代码语言:javascript
复制
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);
    });
  };
});

有人知道这是什么原因吗?请知道如何解决这个问题?

这是一个完全的错误:

代码语言:javascript
复制
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:

代码语言:javascript
复制
{
  "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"
  }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-07-11 14:39:34

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

您想要使用的是:

代码语言:javascript
复制
client.on('error', (err) => { 
   // do whatever.  Though again you're calling a `done` that doesn't seem defined, and some other concerns. 
});
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51287682

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档