0

Im starting to work with async programming, Im making a nodejs application, I´ve slice the code in some files: index.js, ctlUser.js, DAO.js etc... .. Index.js is the main file it requires ctlUser and ctlUser require DAO.js... DAO connect to database and execute queries...

Abstracting, my structure is like this...

Index.js

var ctlUser =  require('./ctlUser.js');
var username = ctlUser.getUserName('1');
console.log("Return from ctlUser" + username);

ctlUser.js

var DAO = require('./DAO.js');
var getUserName = function(id){
    var userName = DAO.executeQuery("SELECT username FROM tbUsers WHERE id = " + id );
    console.log(Return from DAO = userName);
    return username;
}

DAO.js here is everything fine...

var mysql      = require('mysql'); 
var executeQuery = function(query) {    
        var connection = mysql.createConnection({
            host        : SERVER,
            user        : USER,
            password    : PASSWORD,
            database    : DATABASE
        });
        connection.connect();
        connection.query(query, function(err, rows, fields) {
            if (err) throw err;
            connection.end();
            console.log("Here in DAO: " + rows[0].username);
            return rows[0].username;
        });
    };

The output of $ node index.js is:

Return of ctlUser: undefined
Return of DAO: undefined
Here in DAO: Filipe Tagliacozzi

Im abstracting all module exports everithing works fine with fixed variables, but with database response dont..How to implement callbacks in this structure to take the userName to the index.js?

2 Answers 2

8

Change your executeQuery function in DAO.js module to use a callback:

var executeQuery = function(query,callback) {    
        var connection = mysql.createConnection({
            host        : SERVER,
            user        : USER,
            password    : PASSWORD,
            database    : DATABASE
        });
        connection.connect();
        connection.query(query, function(err, rows, fields) {
            if (err) throw err;
            connection.end();
            console.log("Here in DAO: " + rows[0].username);
            callback(rows[0].username);
        });
    };

Chain the callback through your getUserName function in ctlUser.js:

    var getUserName = function(id,callback){
        DAO.executeQuery("SELECT username FROM tbUsers WHERE id = " + id ,function(username){
           console.log("Return from DAO =" ,userName);
           callback(username);
        });       
    }

Then consume it in index.js like so:

var ctlUser =  require('./ctlUser.js');
ctlUser.getUserName('1',function(username){
    console.log("Return from ctlUser" + username);
});
Sign up to request clarification or add additional context in comments.

3 Comments

.. this is what Im talking about... Ill try it right now tnx,,,
Im getting an error "Undefined is not a function" in DAO line of "callback(rows[0].username)"
You were missing quotes on the console.log line of getUserName function in ctlUser.js. I fixed and tested. It's working for me now.
-1

They are undefined because you haven't exported anything.

In node files are executed when you require them but what is returned is what is exported.

what-is-require

in ctlUser.js you need to export getUserName as such :-

exports.getUserName = getUserName;

and in DAO.js you need to export executeQuery as such :-

exports.executeQuery = executeQuery;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.