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 trialLaxmikanta Nayak
Front End Web Development Techdegree Student 554 PointsI cant able to return data from a method
my server.js file:
var http=require('http');
var router= require('./router.js');
var weatherDetails=require('./weatherDetails.js');
var prettyJson=weatherDetails.getWeatherReport('USA');
console.dir(prettyJson);
var server=http.createServer(function(request,response){
response.writeHead(200,{'Content-Type':'text/plain'});
//console.log(request.url);
if(request.url==='/'){
router.home(request,response);
}
else{
router.searchResult(request,response);
}
//response.write(weatherDetails.getWeatherReport('USA'));
response.end("/nend");
}).listen(1337);
server.on("error",function(error){
console.error("Sorry we have this error :: "+error.message);
});
route.js file
function home(request,response){
response.write('SEARCHING');
}
function searchResult(request,response){
response.write('SEARCH RESULT');
}
module.exports.home=home;
module.exports.searchResult=searchResult;
weatherdetails.js
var http=require('http');
var body='';
var prettyJson='';
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);
while(prettyJson.location.name!='undefined'){
console.log('here');
return prettyJson.location.name; <-- on this link i cant able to get the value reutrned to my server.js file
}
});
});
}
module.exports.getWeatherReport=getWeatherReport;
2 Answers
Steven Parker
231,269 PointsThe line you indicated is inside an asynchronous callback function. Where do you expect the return value to go?
The bottom line is your own code doesn't directly call that function, so anything it tries to return will be lost. But you could assign it to some global variable, or call another function to do something with it.
Farid Wilhelm Zimmermann
16,753 PointsIยดd also recommend just putting in another callback function that runs once the data is received, using the received data as parameter.
I had a similiar problem to yours, when I was trying to make an API Call, that relied on JSON from a previous API Call, and got around by just declaring the second API Call inside a function, that got triggered .on("end") inside the callback function of the first API Call.
Laxmikanta Nayak
Front End Web Development Techdegree Student 554 PointsLaxmikanta Nayak
Front End Web Development Techdegree Student 554 PointsActually i want to take that value and use as field in my output , like location name , i have passed that to another function also assigned to global variable but still the same result is 'undefined'.
i want to access the locationname from getWeatherReoport Method from weatherdeails.js file in server.js to show to the use the result