Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.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 如何在Nodejs+;快车_Javascript_Json_Node.js - Fatal编程技术网

Javascript 如何在Nodejs+;快车

Javascript 如何在Nodejs+;快车,javascript,json,node.js,Javascript,Json,Node.js,我知道这是个愚蠢的问题,但我找不到符合我要求的答案。 我试图从返回Json的Web服务中获取数据,并将其添加到其他数据数组中,这些数据是我allready使用一个异步函数在另一个异步函数中从不同的Web服务请求中获取的 代码如下: main.js })) 在model.js中 a如何将数据从第二个回调函数传递到第一个回调函数 关于您不能,除非您将变量提升到内部函数范围之外。但是您仍然需要处理同步问题 请记住,此处将运行异步,因此按照编写函数的方式,res.render可能(并且可能)在调用dat

我知道这是个愚蠢的问题,但我找不到符合我要求的答案。 我试图从返回Json的Web服务中获取数据,并将其添加到其他数据数组中,这些数据是我allready使用一个异步函数在另一个异步函数中从不同的Web服务请求中获取的

代码如下:

main.js }))

在model.js中 a如何将数据从第二个回调函数传递到第一个回调函数


关于

您不能,除非您将变量提升到内部函数范围之外。但是您仍然需要处理同步问题

请记住,此处将运行异步,因此按照编写函数的方式,res.render可能(并且可能)在调用
data.getdata.subs
之前运行

如果这不是在一个
forEach
中,那么很明显,您只需要将res.render放在第二个回调中,但是您需要一些更复杂的东西

我会推荐这个套餐

具体地说,请看“res.render可能(并且可能)会在”保证首先运行之前运行。JavaScript是单线程的-整个外部函数将在任何异步回调发生之前运行到完成。
server.get('/app', function(req, res){

//This code get the categories data from a webservice
data.getdata.categories(userid, function(err, datos){
    datos.forEach(function(dato){

        //This function get data from a diferent webservice that return a Json
        data.getdata.subs(dato.name, function(response){
                if(response){
                    //response.total bring me an integer, this is the variable that i need to pass thru the res.render variables 
                    dataq = response.total; 
                }
                //for each category i need to store the number of subscription (the integer in response.total)
                datos.number = dataq;
            });         

    });

    res.render('app', {
        user : req.session.user,
        usertype: req.session.usertype,
        number : userid,
        listusers: ListUsers,
        listsubs: datos, // Here i get the data without the number value that i tryed to add in 2nd function
        permit: datos.length
    }); 
});
//This one request to a first webservice that return a Json
categories: function(user_id, callback){
        request('http://localhost:3030/getdata/*/subscriptions/user_id/'+user_id, function(error, res, body) {
            if (error) { 
                callback("error", null); 
            }else{
                result = JSON.parse(body);
                callback(null, result);
            }

        });
    },

    //This one request to another webservice that return a Json
    subs: function(cat, callback){
        request('http://104.131.7.130:8080/event/'+cat, function(error, res, body){
            if(error){
                return callback("error", null);
            }else{
                result2 = JSON.parse(body);
                return callback(result2);
            }
        });

    }