【问题标题】:NodeJS error: throw er; // Unhandled 'error' eventNodeJS 错误:抛出错误; // 未处理的“错误”事件
【发布时间】:2019-10-20 17:29:03
【问题描述】:

我创建了一个简单的 React + node-postgres + expressJS + PostgreSQL DB webapp。

它只有两个输入和一个按钮。当我按下按钮时,值将存储在我的数据库中。

ExpressJS 服务器使用 nodemon 运行。如果服务器运行,该应用程序可以正常工作。当我输入第一个值然后按下按钮时,一切都会正确存储 - 一段时间后,nodemon 服务器崩溃并出现如下所示的错误。

C:\node tutorial\reactPSQLexpress\backend>nodemon server.js
[nodemon] 1.19.1
[nodemon] to restart at any time, enter `rs`
[nodemon] watching: *.*
[nodemon] starting `node server.js`
Listening to PORT: 7777
{ country_name: 'test', continent_name: 'test' }
Data INSERT succesfull
events.js:167
      throw er; // Unhandled 'error' event
      ^

Error [ERR_STREAM_WRITE_AFTER_END]: write after end
    at writeAfterEnd (_stream_writable.js:243:12)
    at Socket.Writable.write (_stream_writable.js:291:5)
    at Connection.end (C:\node tutorial\reactPSQLexpress\backend\node_modules\pg\lib\connection.js:339:22)
    at Client.end (C:\node tutorial\reactPSQLexpress\backend\node_modules\pg\lib\client.js:525:21)
    at Pool._remove (C:\node tutorial\reactPSQLexpress\backend\node_modules\pg-pool\index.js:161:12)
    at Timeout.setTimeout (C:\node tutorial\reactPSQLexpress\backend\node_modules\pg-pool\index.js:44:12)
    at ontimeout (timers.js:436:11)
    at tryOnTimeout (timers.js:300:5)
    at listOnTimeout (timers.js:263:5)
    at Timer.processTimers (timers.js:223:10)


Emitted 'error' event at:
    at Client.idleListener (C:\node tutorial\reactPSQLexpress\backend\node_modules\pg-pool\index.js:78:10)
    at Client.emit (events.js:182:13)
    at Connection.connectedErrorHandler (C:\node tutorial\reactPSQLexpress\backend\node_modules\pg\lib\client.js:182:10)
    at Connection.emit (events.js:182:13)
    at Socket.reportStreamError (C:\node tutorial\reactPSQLexpress\backend\node_modules\pg\lib\connection.js:72:10)
    at Socket.emit (events.js:182:13)
    at writeAfterEnd (_stream_writable.js:245:10)
    at Socket.Writable.write (_stream_writable.js:291:5)
    at Connection.end (C:\node tutorial\reactPSQLexpress\backend\node_modules\pg\lib\connection.js:339:22)
    at Client.end (C:\node tutorial\reactPSQLexpress\backend\node_modules\pg\lib\client.js:525:21)

[nodemon] app crashed - waiting for file changes before starting...

我已经杀死了所有节点服务器并重新启动了所有内容,但是在插入一个值之后,同样的情况发生了。

taskkill /f /im node.exe

kill 命令运行良好 - 但问题仍然存在。

编辑:服务器端代码:

app.use(bodyParser.json());
app.use(
  bodyParser.urlencoded({
    extended: true
  })
);

app.use((req, res, next) => {
  res.header("Access-Control-Allow-Origin", "*");
  res.header(
    "Access-Control-Allow-Headers",
    "Origin, X-Requested-With, Content-Type, Accept"
  );
  next();
});

app.post("/api/new-country", (req, res) => {
  console.log(req.body);
  let country_name = req.body.country_name;
  let continent_name = req.body.continent_name;

  pool.connect((err, db, done) => {
    if (err) {
      return res.status(400).send(err)
    } else {

      db.query("INSERT INTO country (country_name, continent) VALUES($1, $2)",[country_name, continent_name], (err, table) =>{
    //   db.query("SELECT * from country", (err, table) => {
        done();
        if (err) {
          return res.status(400).send(err) 
        } else {
        //   console.log(table.rows);
          console.log("Data INSERT succesfull");
          db.end();
          res.status(201).send({message: "Data inserted!"})
        }
      });
    }
  }); 
});

app.listen(PORT, () => console.log(`Listening to PORT: ${PORT}`));

我将 db.end 移到 res.status(201) 后面...但没有帮助。

【问题讨论】:

  • write after end 表示快速服务器正在写回已结束请求的响应。你能粘贴你的服务器端代码吗
  • 向我们展示您的代码
  • 我编辑了这个问题 - 我只是遗漏了我的池设置和文件中的 require 导入。感谢您的回复。
  • done() 函数里面是什么?
  • @AnshulJindal + Roy.B -- 我将函数放在 pool.connect 范围内并删除了 done();打电话,因为它实际上什么也没做。服务器现在没有崩溃,一切似乎都正常。谢谢大家

标签: javascript node.js json reactjs


【解决方案1】:

我遇到了同样的错误,结果发现我的 .env 文件中有错字。 我写了

PORT=2000; #instead of PORT=2000

【讨论】:

  • 我对端口的监听也略有不同:console.log(Listening to PORT: ${process.env.PORT});
猜你喜欢
  • 2020-10-10
  • 1970-01-01
  • 2019-01-08
  • 2019-05-04
  • 2018-10-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-05-25
相关资源
最近更新 更多