Node.js/MySQL: Printing actual query in error log in node.js

As per docs, You can use query.sql to get the actual executed query.

var post  = {id: 1, title: 'Hello MySQL'};
var query = connection.query('INSERT INTO posts SET ?', post, function(err, result) {
  // Neat!
});
console.log(query.sql); // INSERT INTO posts SET `id` = 1, `title` = 'Hello MySQL'

In this case, it will be

connection.query(command, function (err, rows) {
    if (err) {
        console.log('this.sql', this.sql); //command/query
        console.log(command);
        console.log("ERROR");
        console.log(err);
        return;
    }
    console.log("good");
});

If @Sridhar answer doesn't work for you, probably because you are using promise API which doesn't yet return the SQL query, you can use:

const sql = connection.format("SELECT * FROM table WHERE foo = ?", ["bar"]);
console.log(sql);
const [rows] = await connection.query(sql);

Documentation: https://github.com/mysqljs/mysql#preparing-queries

Tags:

Mysql

Node.Js