Tuesday, 25 April 2017

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



via joe

No comments:

Post a Comment