简体   繁体   中英

passing argument from one function to request.on nodejs

i am newbie to nodejs/javascript and trying to pass a value from a function.

Below is my request.on function ( i need to get favoriteStn value and pass it on while building the JSON array)

  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

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.

in your case findifFavorite execution time is taking more time and the value is not available for the array.

this is how you can fix your code, you have to move the resToSend array in the callback function.

Start reading more about promises https://github.com/petkaantonov/bluebird so you will not get stuck in callback hell.

                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);


                });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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