admin管理员组

文章数量:1336403

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?

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?

Share edited May 14, 2013 at 14:26 Filipe Tagliacozzi asked May 14, 2013 at 14:20 Filipe TagliacozziFilipe Tagliacozzi 1,4312 gold badges20 silver badges28 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 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);
});

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;

本文标签: javascriptHow to callback function from another file in nodejsStack Overflow