简体   繁体   English

将参数从一个函数传递到request.on nodejs

[英]passing argument from one function to request.on nodejs

i am newbie to nodejs/javascript and trying to pass a value from a function. 我是nodejs / javascript的新手,并尝试从函数传递值。

Below is my request.on function ( i need to get favoriteStn value and pass it on while building the JSON array) 下面是我的request.on函数(在构建JSON数组时,我需要获取favoriteStn值并将其传递给它)

  response.on('end', function () {
             str = JSON.parse(str);
        var summariesJSON = str[0].summaries
          var resToSend = [];

            for(var i in summariesJSON) {

                var item = summariesJSON[i];
                var favoriteStn = findifFavorite (usernameFrmQuery,item.device_id,function(value){
                        favoriteStn = value;

                    });
                    resToSend.push({ 
                    "lat" : item.lat,
                    "lng"  : item.ln,
                    "count"       : item.count,
                    "status" : item.status,
                    "id" :   item.id,
                    "name"  :   item.name,
                    "address"   :   item.address,
                    "favoriteStn" : favoriteStn,
                    "fav_count" : findFavCount
                });
            } 
    res.send(resToSend);
  });


function findifFavorite (username,stationId,cb) {
    var options = {
    };
            ddb.getItem(chgStationfavorite, username, String(stationId), options, function(err, getitemRes, cap) {
            if (err) {
                cb("Failure" + err);
                } else if(typeof(getitemRes) != 'undefined'){

                cb("Y");
            }
                else {

                cb("N");
                }

        });

}

issue is i dont get anything created for favoriteStn, i know it is getting into the function and providing values as i can see it thru console.log 问题是我什么都没有为favoriteStn创建任何东西,我知道它正在进入函数并提供值,因为我可以通过console.log看到它

can you help me on how i need to use callback and get it working? 您能帮我如何使用回调并使它正常工作吗?

you have to consider everything in node.js is asynchronous. 您必须考虑到node.js中的所有内容都是异步的。

in your case findifFavorite execution time is taking more time and the value is not available for the array. 在您的情况下, findifFavorite执行时间花费更多时间,并且该值不适用于该数组。

this is how you can fix your code, you have to move the resToSend array in the callback function. 这是修复代码的方法,您必须在回调函数中移动resToSend数组。

Start reading more about promises https://github.com/petkaantonov/bluebird so you will not get stuck in callback hell. 开始阅读有关Promise的更多信息https://github.com/petkaantonov/bluebird,这样您就不会陷入回调地狱。

                var favoriteStn = findifFavorite (usernameFrmQuery,item.device_id,function(value){
                    favoriteStn = value;

                        resToSend.push({ 
                           "lat" : item.lat,
                            "lng"  : item.ln,
                           "count"       : item.count,
                           "status" : item.status,
                           "id" :   item.id,
                           "name"  :   item.name,
                           "address"   :   item.address,
                           "favoriteStn" : favoriteStn,
                           "fav_count" : findFavCount
                         });

                        res.send(resToSend);


                });

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

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