简体   繁体   中英

Node.js - events.js throw er

Before posting this question I went through a couple of SO posts with the same heading. All of them suggested me to restart my app..I tried that but I am still getting the error.

The code that is throwing the error is

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();
});

The error caused by the code is this

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)

I am hosting my app on Heroku and whenever I try to access this URL the app is crashing forcing me to restart the dyno. The data I am sending is being successfully inserted into the DB but the auto incremented primary key is not being returned to my app. Instead I get a server error.

JSON.stringify() returns the correct value once and after that the app crashed which maked the subsequent requests to fail.

How can I solve this problem?

Try converting your response into JSON before sending it.

Like

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

I too once had a similar problem and I think that is how I solved it.

EDIT:

I went through the myql npm library and found something called Pool . Pool is used when you app needs to handle more than one request at a time. I suggest that you try that. There is a good example availble here.

I have same problem, i just downgrade to react-scripts@2.1.8

this is the steps:

  1. cd my-app
  2. npm install react-scripts@2.1.8
  3. npm start

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM