Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/409.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_Asynchronous - Fatal编程技术网

Javascript node.js在函数之间异步传递变量

Javascript node.js在函数之间异步传递变量,javascript,node.js,asynchronous,Javascript,Node.js,Asynchronous,我在node.js上迈出了第一步,在异步传递变量时遇到了这个问题。我用这段代码创建Facebook用户: req.tmpPassport = {}; var fb = new fbgraph.Facebook(accessToken, 'v2.2'); function initUser() { fb.me(function (err, me) { req.tmpPassport.me = me; console.log(req.tmpPassport.m

我在node.js上迈出了第一步,在异步传递变量时遇到了这个问题。我用这段代码创建Facebook用户:

req.tmpPassport = {};
var fb = new fbgraph.Facebook(accessToken, 'v2.2');
function initUser() {
    fb.me(function (err, me) {
        req.tmpPassport.me = me;
        console.log(req.tmpPassport.me) // works
    });
}

console.log(req.tmpPassport.me) // not working -> undefined var
我试图找出第二个日志不起作用的原因,最后阅读了有关同步和异步函数的内容,因此,为了实现我所阅读的内容,我尝试使用回调来提出解决方案,但没有成功。 我最后一次尝试是:

req.tmpPassport = {};
var fb = new fbgraph.Facebook(accessToken, 'v2.2');
function initUser() {
    fb.me(function (err, me) {
        req.tmpPassport.me = me;
    });
    fb.my.events(function (err, events) {
        //console.log(events);
        req.tmpPassport.events = events;

    });
    fb.my.friends(function (err, result) {
        req.tmpPassport.results = result;
    });
}
function passUser(){
    console.log(req.tmpPassport);
    return req.tmpPassport;
}
cp.exec(initUser, passUser);
但它不起作用。。。 我实际上是想用我的express router var渲染这个对象,它看起来像这样:

   router.get('/welcome', securePages, function(req, res, next){
        res.render('welcome', {title:'Welcome to aDating', user:req.tmpPassport});
    })

但是我不知道只有在创建之后如何传递这个对象……请提供帮助?

在完成某些异步任务时链接函数调用的方法是解决这个问题的一种方法

查看第一段代码,它将重写如下:

req.tmpPassport = {};
var fb = new fbgraph.Facebook(accessToken, 'v2.2');
function initUser() {
    fb.me(function (err, me) {
        console.log(req.tmpPassport.me) // works
        req.tmpPassport.me = me;

        // triggers execution of the next step
        post_populating_passport();
    });
}

function post_populating_passport() {
    // this function is only executed after the callback from the async call
    console.log(req.tmpPassport.me);
}

在完成某些异步任务时链接函数调用的方法是处理此问题的一种方法

查看第一段代码,它将重写如下:

req.tmpPassport = {};
var fb = new fbgraph.Facebook(accessToken, 'v2.2');
function initUser() {
    fb.me(function (err, me) {
        console.log(req.tmpPassport.me) // works
        req.tmpPassport.me = me;

        // triggers execution of the next step
        post_populating_passport();
    });
}

function post_populating_passport() {
    // this function is only executed after the callback from the async call
    console.log(req.tmpPassport.me);
}

在完成某些异步任务时链接函数调用的方法是处理此问题的一种方法

查看第一段代码,它将重写如下:

req.tmpPassport = {};
var fb = new fbgraph.Facebook(accessToken, 'v2.2');
function initUser() {
    fb.me(function (err, me) {
        console.log(req.tmpPassport.me) // works
        req.tmpPassport.me = me;

        // triggers execution of the next step
        post_populating_passport();
    });
}

function post_populating_passport() {
    // this function is only executed after the callback from the async call
    console.log(req.tmpPassport.me);
}

在完成某些异步任务时链接函数调用的方法是处理此问题的一种方法

查看第一段代码,它将重写如下:

req.tmpPassport = {};
var fb = new fbgraph.Facebook(accessToken, 'v2.2');
function initUser() {
    fb.me(function (err, me) {
        console.log(req.tmpPassport.me) // works
        req.tmpPassport.me = me;

        // triggers execution of the next step
        post_populating_passport();
    });
}

function post_populating_passport() {
    // this function is only executed after the callback from the async call
    console.log(req.tmpPassport.me);
}

js是一种异步编程语言,是其核心的基本概念。对于异步流,您需要使用函数回调(不是一个好的做法)或可用的npms实用程序。

Node.js是一种异步编程语言,是其核心的基本概念。对于异步流,您需要使用函数回调(不是一个好的做法)或可用的npms实用程序。

Node.js是一种异步编程语言,是其核心的基本概念。对于异步流,您需要使用函数回调(不是一个好的做法)或可用的npms实用程序。

Node.js是一种异步编程语言,是其核心的基本概念。对于异步流,您需要使用函数回调(这不是一个好的做法)或可用的npms实用程序