Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

427
Views
mysql insert not working in node js

I am developing a web app using nodejs and mysql. The mysql connection is working as fine but query is not excecuting and not showing any errors. My code ishown below. Pls help me.

var mysql = require("mysql");
var con = mysql.createConnection({
host: "localhost",
user: "root",
password: "",
database : "db"
});

con.connect(function(err){
if(err){
        console.log('Error connecting to Db');

    return;
} else {


var time = Date.now();
var post  = {userphone: "user", senderphone: "to1", message: "msg", time:time};             
var query = con.query('INSERT INTO chat SET ?', post, function(err, result) {
    console.log('db added');
    console.log(query.sql);
            });
        console.log('Connection established');
    }
});

con.end(function(err) {
    // The connection is terminated gracefully
    // Ensures all previously enqueued queries are still
    // before sending a COM_QUIT packet to the MySQL server.
});
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

If you try to print the err in the connection it shows:

    ~/Documents/src : $ node testInsert.js 
Connection established
{ [Error: Cannot enqueue Query after invoking quit.] code: 'PROTOCOL_ENQUEUE_AFTER_QUIT', fatal: false }
db added
INSERT INTO chat SET `userphone` = 'user', `senderphone` = 'to1', `message` = 'msg', `time` = 1493132496360

Clearly your database connection is getting closed before executing query. You need to move that close connection block into query block to force it's execution after query like this:

con.connect(function(err){
  console.log('Connection established');
  if(err){
      console.log('Error connecting to Db');
      return;
  } else {
    var time = Date.now();
    var post  = {userphone: "user", senderphone: "to1", message: "msg", time:time};             
    var query = con.query('INSERT INTO chat SET ?', post, function(err, result) {
      if(err) console.log(err);
        console.log('db added');
        console.log(query.sql);
        con.end(function(err) {
            // The connection is terminated gracefully
            // Ensures all previously enqueued queries are still
            // before sending a COM_QUIT packet to the MySQL server.
        });
    });
  }
});

When i ran it insertion was successful:

~/Documents/src : $ node testInsert.js 
Connection established
db added
INSERT INTO chat SET `userphone` = 'user', `senderphone` = 'to1', `message` = 'msg', `time` = 1493132632444
mysql> SELECT * FROM chat;
+-----------+-------------+---------------------+---------+
| userphone | senderphone | time                | message |
+-----------+-------------+---------------------+---------+
| user      | to1         | 0000-00-00 00:00:00 | msg     |
+-----------+-------------+---------------------+---------+
1 row in set (0.00 sec)
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!