I have a syntax problem in a module, I fail to do the SQL query.
I initialize the module database in file.js, it responds with console.log 'Connected to the database', then sends the data to the module in Database.newData(data), when it enters in runQuery nothing happens, no errors or result, nothing!
I look in runQuery if this query was ok and if this, I think what happens is that there is an error in my logic of node, the idea is to connect to the database and use runQuery to run any query that you pass.
file.js
var DB = require('./modules/database');
var Database = new DB();
Database.newData(data);
database.js
var mysql = require('mysql'),
queries = require('./queries'),
connection;
var DB = function(){
var db_config = {
host: 'localhost',
user: 'diegoug',
password: 'qwertyuiop',
database: 'test'
};
connection = mysql.createConnection(db_config);
connection.connect(function(err) {
if(err) {
console.log('error when connecting to database:', err);
}
console.log('Connected to the database');
});
}
DB.prototype.runQuery = function(Query,Data,cb){
// Here not pass nothing
connection.query(
Query,
Data,
function(err, results){
debugger;
if (err)throw err;
cb(results);
}
);
// look here if the query was well written and if it is, what happens is that it's simply not running anything in the connection
}
DB.prototype.newData = function(data){
var Query = queries.SQLNEWDATA,
data = [data];
var res = this.runQuery(Query,data);
console.log(res);
}
module.exports = DB;