0

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;
user987055
  • 1,039
  • 4
  • 14
  • 25
  • You don't show how you are calling this, which would be relevant. However, if I had to guess the problem most probably is that you're connection hasn't been established by the time runQuery is being invoked, since it doesn't appear that you provide any way for the calling code to learn that the connection has been established. – barry-johnson Mar 10 '14 at 01:22
  • the file.js calls the module database.js – user987055 Mar 10 '14 at 02:44
  • @user987055 I don't think you understand. What's going on here is the database connection does not block execution, therefore you're not guaranteed to have a valid database connection until the callback on the `connect()` method is fired and `err` is falsey. Node.js is asynchronous by design - you need to reorganize your code using callbacks to prevent any queries against the database until a connection has been established. – SamT Mar 10 '14 at 02:57
  • you're right, I do not understand, that way I can sort the code and use callbacks? for me this is the most advanced I've come to understand the modules in node. – user987055 Mar 10 '14 at 03:14
  • what do you see as output? does it ever print "Connected to database"? – AJcodez Mar 10 '14 at 04:13
  • I need runQuery bring me the answer of any query to the database. – user987055 Mar 10 '14 at 15:39

0 Answers0