0

I just need a way to insert simple data in the database.

This is my code:

'use strict'
const express = require('express')
const bodyParser = require('body-parser')
const request = require('request')
const app = express()
const mysql = require('mysql')


var connection = mysql.createConnection({  
  host     : 'xxx',  
  user     : 'xxx',  
  password : 'xxx',  
  database : 'xxx'  
});  

connection.connect(function(err) {
  if (err) {
    console.error('error connecting: ' + err.stack);
    return;
  }

  console.log('connected as id ' + connection.threadId);
}); 


app.set('port', (process.env.PORT || 5000))

// Process application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({extended: false}))

// Process application/json
app.use(bodyParser.json())

// Index route
app.get('/', function (req, res) {
   res.send('Hello world')
})

    app.get('/adduser', function (req, res) {

       var queryaggiungi = 'INSERT INTO users (psid, name, surname, gender, timezone, locale ) VALUES (11111111111111111, "aaa", "bbb", "male", 3, "IT_en");';

       connection.query(queryadd, function (err, result) {
          //if (err) throw err; 
          console.log("user added");
          connection.end();
       }); 

       res.send('query: ' + queryadd);
    })


// Spin up the server
app.listen(app.get('port'), function() {
    console.log('running on port', app.get('port'))
})

The first time that I run xxx.com/adduser, then the row is added and the output is this:

2017-10-19T21:22:37.062633+00:00 heroku[router]: at=info method=GET path="/adduser" host=xxx-xxx-xxx.herokuapp.com request_id=384b7896-16b6-42de-9963-79ffc181ad12 fwd="151.70.34.90" dyno=web.1 connect=0ms service=18ms status=304 bytes=150 protocol=https
2017-10-19T21:22:37.062148+00:00 app[web.1]: user added

After that, every time I run xxx.com/adduser the row is not added anymore and I get this:

2017-10-19T21:23:17.948224+00:00 app[web.1]: user added
2017-10-19T21:23:17.950545+00:00 app[web.1]: events.js:161
2017-10-19T21:23:17.950547+00:00 app[web.1]:       throw er; // Unhandled 'error' event
2017-10-19T21:23:17.950547+00:00 app[web.1]:       ^
2017-10-19T21:23:17.950547+00:00 app[web.1]: 
2017-10-19T21:23:17.950548+00:00 app[web.1]: Error: Cannot enqueue Quit after invoking quit.
2017-10-19T21:23:17.950549+00:00 app[web.1]:     at Protocol._validateEnqueue (/app/node_modules/mysql/lib/protocol/Protocol.js:204:16)
2017-10-19T21:23:17.950549+00:00 app[web.1]:     at Protocol._enqueue (/app/node_modules/mysql/lib/protocol/Protocol.js:139:13)
2017-10-19T21:23:17.950550+00:00 app[web.1]:     at Protocol.quit (/app/node_modules/mysql/lib/protocol/Protocol.js:92:23)
2017-10-19T21:23:17.950550+00:00 app[web.1]:     at Connection.end (/app/node_modules/mysql/lib/Connection.js:249:18)
2017-10-19T21:23:17.950551+00:00 app[web.1]:     at Query._callback (/app/index.js:128:18)
2017-10-19T21:23:17.950551+00:00 app[web.1]:     at Query.Sequence.end (/app/node_modules/mysql/lib/protocol/sequences/Sequence.js:88:24)
2017-10-19T21:23:17.950552+00:00 app[web.1]:     at /app/node_modules/mysql/lib/protocol/Protocol.js:225:14
2017-10-19T21:23:17.950553+00:00 app[web.1]:     at _combinedTickCallback (internal/process/next_tick.js:67:7)
2017-10-19T21:23:17.950553+00:00 app[web.1]:     at process._tickCallback (internal/process/next_tick.js:98:9)
2017-10-19T21:23:17.952807+00:00 heroku[router]: at=info method=GET path="/adduser" host=tranquil-woodland-21158.herokuapp.com request_id=cb522acc-a134-4fb2-bf21-64ce97e9584a fwd="11.10.34.30" dyno=web.1 connect=0ms service=7ms status=304 bytes=150 protocol=https
2017-10-19T21:23:18.034002+00:00 heroku[web.1]: State changed from up to crashed
2017-10-19T21:23:18.020653+00:00 heroku[web.1]: Process exited with status 1

What is the problem ?

I get this problem with JawsDB Mysql and with ClearDB heroku addons.

Thanks at all :)

EDIT:

CREATE TABLE IF NOT EXISTS users ( 
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, 
psid BIGINT, 
name VARCHAR(20), 
surname VARCHAR(20), 
gender VARCHAR(6), 
timezone INT, 
locale VARCHAR(6), 
inserted TIMESTAMP DEFAULT CURRENT_TIMESTAMP 
);
xRobot
  • 25,579
  • 69
  • 184
  • 304
  • Can you post the DDL for the table you are trying to insert to? – Saïd Oct 19 '17 at 21:32
  • @Saheed added above :) – xRobot Oct 19 '17 at 21:37
  • the DDL looks OK but the snippet of Node.js code looks incomplete. Can you post a minimum working snippet for `/adduser`? – Saïd Oct 19 '17 at 21:47
  • See also this [answer](https://stackoverflow.com/questions/14087924/cannot-enqueue-handshake-after-invoking-quit/16365821#16365821) about using connection pools as it appears you are ending the database connection on each request. – Saïd Oct 19 '17 at 21:58
  • @Saheed the code is complete. I get that error with that code. What do you mean exactly ? – xRobot Oct 19 '17 at 21:59
  • The `require`s and several other declarations are missing; essentially, the code you posted is not enough for me to independently reproduce the error. – Saïd Oct 19 '17 at 22:02
  • @Saheed I removed "connection.end();" and now it works. Is it secure ? I have just added the rest of the code above :) – xRobot Oct 19 '17 at 22:08

0 Answers0