Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript Build a Simple Dynamic Site with Node.js HTTP Methods and Headers Dealing with the POST Body

I want to get the complete response of asynchronous request and want to use that in other functions

please have alook to the function below : getLocationName to see my question in detail

var http=require('http');
var body='';
var prettyJson={};
var locationName='';
var temperature='';
var pressure='';
var humidity='';
var parsedBody='';
function getWeatherReport(country){
    var request=http.get("http://api.apixu.com/v1/current.json?key=5fccf98036ed460abb8195912162406&q="+country,function(response){
        response.on("data",function(chunk){
            body+=chunk;
        });
        response.on("end",function(){
        prettyJson=JSON.parse(body);
                getLocationName(prettyJson);
    },"2000");


        });
        response.on("error",function(error){
            console.error(error);
        });

    });


}
function  getLocationName(){
    console.log(prettyJson);
//i want to use this object in all other pages  but the error i am facing is :
"i cant able to get the complete response as it is an async call"//
}
module.exports.getWeatherReport=getWeatherReport;
module.exports.getLocationName=getLocationName;

2 Answers

Thomas Nilsen
Thomas Nilsen
14,957 Points

You could add a callback to your function, that would look like this:

var http    = require('http');
var url     = 'http://api.apixu.com/v1/current.json?key=5fccf98036ed460abb8195912162406&q=';


function getWeatherReport(country, callback) {
    var data = null;
    http.get(url + country, function(response) {
        response.on('data', function(chunk) {
            data += chunk;
        });

        response.on('end', function() {
            callback(null, data);
        });
    }).end('error', function(err) {
        callback(err, null);
    });
}


module.exports = getWeatherReport;

And when you want to use this in another file you write:

var weather = require('weather');

weather('norway', function(err, data) {
    console.log(data);
});
Steven Parker
Steven Parker
231,275 Points

You have a couple of options:

  • you can call your other functions from inside the handler where the value is available to be passed.
  • during the handler you can store the value into a global variable that other functions can access.

Thanks Steven for your answer. But in that case also the full response i am not getting it is only a chunk i am getting , can you please write the code you are talking about that would be really helpful steven.