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 badges2 Answers
Reset to default 8Change 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
版权声明:本文标题:javascript - How to callback function from another file in nodejs? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742404714a2468584.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论