简体   繁体   English

如何从Node.js中的另一个文件回调函数?

[英]How to callback function from another file in nodejs?

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... 我开始使用异步编程,正在制作一个nodejs应用程序,已经将代码切到了一些文件中:index.js,ctlUser.js,DAO.js等.. Index.js是它需要的主要文件ctlUser和ctlUser需要DAO.js ... DAO连接到数据库并执行查询...

Abstracting, my structure is like this... 抽象,我的结构就是这样...

Index.js Index.js

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

ctlUser.js 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... DAO.js一切都很好...

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: $ node index.js的输出是:

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? 我对所有模块导出进行抽象都可以使用固定变量,但不能使用数据库响应。.如何在此结构中实现回调以将userName用作index.js?

Change your executeQuery function in DAO.js module to use a callback: 在DAO.js模块中更改您的executeQuery函数以使用回调:

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: 通过ctlUser.js中的getUserName函数链接回调:

    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: 然后像这样在index.js中使用它:

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 :- 在ctlUser.js中,您需要这样导出getUserName:

exports.getUserName = getUserName;

and in DAO.js you need to export executeQuery as such :- 在DAO.js中,您需要将executeQuery导出为:

exports.executeQuery = executeQuery;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM