0

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.
});
9
  • INSERT SET is not standard sql Commented Apr 25, 2017 at 14:54
  • I use VALUES instead of SET But same problem. Commented Apr 25, 2017 at 14:55
  • Did you checked what is coming in err variable? I mean here - query = con.query(..INSERT QUERY.., post, function(err, result) { if(err){throw err;} else{ .... } Commented Apr 25, 2017 at 14:56
  • You have to use INSERT INTO table (field1, field2) VALUES (value1, value2) Commented Apr 25, 2017 at 14:56
  • @ManikandanS Not showing any error Commented Apr 25, 2017 at 14:58

1 Answer 1

2

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)
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.