javascript - Function should return string when called, but returns undefined instead -


this question has answer here:

background

i'm using node.js build small app reads weather data api. i'm using 2 files called router.js , weather.js.

router.js deals routes related web application (e.g. www.myappdomain.com/routeexample).

weather.js interacts weather api , has functions weather data, parses data, , formulates readable string.

router.js

here code in router.js:

const express = require("express"); const router = express.router(); const weather = require("./weather");  router.get('/', (req, res) => {   const {location} = req.query;   console.log(location); // prints query submitted user   let weatherstring = weather.getweatherdata(location);   console.log(weatherstring); // returns undefined   res.render("index", weatherstring);   res.end(); });  module.exports = router; 

inside of callback function request, can see location variable outputs correct string. however, when calling getweatherdata function using location variable argument, returns undefined value.

weather.js

now let's take inside of weather.js see getweatherdata function looks like. i've excluded of code conciseness.

function printweather(location, temperature) {   let string = `the weather ${location} ${temperature} degrees celcius.`;   return string; }  function parseweatherdata(weatherdata) {   let locationstring = weatherdata.name + ", " + weatherdata.sys.country;   let temperature = weatherdata.main.temp;   printweather(locationstring, temperature); } function getweatherdata(location) {    const request = http.get(`http://api.openweathermap.org/data/2.5/weather?q=${location}&appid=${api["key"]}&units=metric`, response => {     if (response.statuscode === 200) {       let body = "";       response.on("data", (data) => {         body += data.tostring();       });       response.on("end", () => {         const weatherdata = json.parse(body);;         parseweatherdata(weatherdata);       });     }   }); }  module.exports.getweatherdata = getweatherdata; 

as can see, when getweatherdata called in router.js, starts executing getweatherdata calls parseweatherdata function. when parseweatherdata gets executed, calls printweather function. printweather function stores readable string in variable , function returns string.

my problem string gets created isn't being returned router.js when getweatherdata function called.

what i've tried

i've been troubleshooting getweatherdata, parseweatherdata, , printweather functions console.log statements, , outputting correct values inside of weather.js. trouble returning final string inside of printweather router.js.

http.get asynchronous call. pass in function callback getweatherdata , call when response complete.

weather.js

function printweather(location, temperature) {    let string = `the weather ${location} ${temperature} degrees celcius.`;    return string;  }    function parseweatherdata(weatherdata) {    let locationstring = weatherdata.name + ", " + weatherdata.sys.country;    let temperature = weatherdata.main.temp;    return printweather(locationstring, temperature);  }  function getweatherdata(location, done) {     const request = http.get(`http://api.openweathermap.org/data/2.5/weather?q=${location}&appid=${api["key"]}&units=metric`, response => {      if (response.statuscode === 200) {        let body = "";        response.on("data", (data) => {          body += data.tostring();        });        response.on("end", () => {          const weatherdata = json.parse(body);;          const formattedweather = parseweatherdata(weatherdata);          done(null, formattedweather);        });      }    });  }    module.exports.getweatherdata = getweatherdata;

router.js

const express = require("express");  const router = express.router();  const weather = require("./weather");    router.get('/', (req, res) => {    const {location} = req.query;    /*second argument getweatherdata anonymous function. err node convention, result second argument in callback */    weather.getweatherdata(location, (err, weatherstring) => {      res.render("index", weatherstring);      res.end();    });  });    module.exports = router;


Comments

Popular posts from this blog

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

python Tkinter Capturing keyboard events save as one single string -

sql server - Why does Linq-to-SQL add unnecessary COUNT()? -