Node.js – events.js抛出呃

在发布这个问题之前,我经历了几个相同标题的SOpost。 他们都build议我重新启动我的应用程序..我试过,但我仍然得到错误。

抛出错误的代码是

app.post('/insert', function(request, response) { var connection = mySequel.createConnection(ConnectionParams); connection.connect(); var UserName = request.body.UserName; // insert into userrecords values ("123456",DEFAULT,DEFAULT,DEFAULT,10); var InsertQuery = "insert into userrecords values (" + UserName + ",DEFAULT,DEFAULT,DEFAULT,-10);"; connection.query(InsertQuery, function(error, result, fields) { if (error) { response.send("error"); console.log(error); throw error; } else { // response.send("success"); console.log(result.insertId); response.send(result.insertId); } }); connection.end(); }); 

代码造成的错误是这样的

 events.js:160 2017-04-26T11:42:52.410094+00:00 app[web.1]: 2017-04-26T11:42:52.410092+00:00 app[web.1]: throw er; // Unhandled 'error' event 2017-04-26T11:42:52.410096+00:00 app[web.1]: Error: Quit inactivity timeout 2017-04-26T11:42:52.410097+00:00 app[web.1]: at Quit.<anonymous> (/app/node_modules/mysql/lib/protocol/Protocol.js:160:17) 2017-04-26T11:42:52.410093+00:00 app[web.1]: ^ 2017-04-26T11:42:52.410098+00:00 app[web.1]: at emitNone (events.js:86:13) 2017-04-26T11:42:52.410099+00:00 app[web.1]: at Quit._onTimeout (/app/node_modules/mysql/lib/protocol/sequences/Sequence.js:127:8) 2017-04-26T11:42:52.410099+00:00 app[web.1]: at Quit.emit (events.js:185:7) 2017-04-26T11:42:52.410100+00:00 app[web.1]: at ontimeout (timers.js:380:14) 2017-04-26T11:42:52.410101+00:00 app[web.1]: at tryOnTimeout (timers.js:244:5) 2017-04-26T11:42:52.410102+00:00 app[web.1]: at Timer.listOnTimeout (timers.js:214:5) 

我在Heroku上托pipe我的应用程序,每当我尝试访问这个URL时,应用程序崩溃迫使我重新启动测功机。 我正在发送的数据已成功插入到数据库中,但自动递增的主键不会被返回到我的应用程序。 相反,我得到一个服务器错误。

JSON.stringify()返回正确的值一次,之后,应用程序崩溃,后来的请求失败。

我怎么解决这个问题?

在发送之前尝试将您的响应转换为JSON。

喜欢

response.send(JSON.stringify(result.insertId));

我也曾经有类似的问题,我认为这就是我解决它的方法。

编辑:

我经历了myql npm库,发现了一些名为Pool东西。 当您的应用程序需要同时处理多个请求时使用池。 我build议你尝试一下。 这里有一个很好的例子。