Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/412.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/41.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Node.js获取函数的返回值_Javascript_Node.js_Callback_Return - Fatal编程技术网

Javascript Node.js获取函数的返回值

Javascript Node.js获取函数的返回值,javascript,node.js,callback,return,Javascript,Node.js,Callback,Return,我刚开始研究node.js并开始了解它的概念,我在理解回调时遇到了一点困难,我试图做的是调用函数getUserBranch(),在函数getOffers()中 我了解到,由于node.js的异步特性,在完成执行后,最好使用回调函数来获取所需的数据 现在,我在检索getUserBranch返回的值时遇到了问题,我不知道如何执行,回调函数具有该值,但如何从中获取该值 file2.js file1.js 在异步调用中,使用堆栈中的回调函数。看看这个: var getUserBranch = funct

我刚开始研究node.js并开始了解它的概念,我在理解回调时遇到了一点困难,我试图做的是调用函数getUserBranch(),在函数getOffers()

我了解到,由于node.js的异步特性,在完成执行后,最好使用回调函数来获取所需的数据

现在,我在检索getUserBranch返回的值时遇到了问题,我不知道如何执行,回调函数具有该值,但如何从中获取该值

file2.js

file1.js


在异步调用中,使用堆栈中的回调函数。看看这个:

var getUserBranch = function(email,callback) {
    client.execute('SELECT * from branch WHERE email=?', [ email ], function(err, data, fields) {
        if (err)
            console.log("error");
        else{
            console.log('The solution is in branch: \n', data);
            /* This data stack 3  */
            callback(data.rows[0];);
        }
    });
};

var getOffers = function (email, callback) {
    var branchObj = require('./file2.js');
    branchObj.getUserBranch(email, function(data){
        /* This data stack 2  */
        callback(data);
    });
};

function anyFunction(){
    getOffers("xx@xxx.com", function(data){
        /* This data stack 1  */
        console.log(data);
    });
}
var getOffers = function (email) {

   var branchObj = require('./file2.js');
   var branchList = branchObj.getUserBranch(email,getList));
   return branchList;
};

var getList = function(res){
  var results=res;
  return results;
}
var getUserBranch = function(email,callback) {
    client.execute('SELECT * from branch WHERE email=?', [ email ], function(err, data, fields) {
        if (err)
            console.log("error");
        else{
            console.log('The solution is in branch: \n', data);
            /* This data stack 3  */
            callback(data.rows[0];);
        }
    });
};

var getOffers = function (email, callback) {
    var branchObj = require('./file2.js');
    branchObj.getUserBranch(email, function(data){
        /* This data stack 2  */
        callback(data);
    });
};

function anyFunction(){
    getOffers("xx@xxx.com", function(data){
        /* This data stack 1  */
        console.log(data);
    });
}