1

I have a function which reads the data from a web page and post it in the console,but I want to return the data which I've read and do somethink else with it.So instead of console.log(temperature) I want to return the temperature value and do console.log(download(url)) but It post 'undefined'.

//require https module
var https=require("https");
//require cheerio to use jquery-like and return a DOM tree
var cheerio=require('cheerio');
var global="";
//a function with url and callback par which connects to URL API and read the data
module.exports.download=function(url){
  //read the data\
  https.get(url,function(res){
    var data="";
    //add it to the data string
    res.on('data',function(chunk){
      data+=chunk;
    });
    //parse it with a callback function
    res.on('end',function(){
      var $=cheerio.load(data);
      var temperature=$("span.temp.swip").text();
     console.log(temperature);
    });
  }).on('error',function(err){
    console.log(err.message)
  });
}

//chose the url to connect
var Ploiesti='44.9417,26.0237';
var Brasov='45.597,25.5525';
var url='https://darksky.net/forecast/' + Ploiesti + '/si24/en';

//download(url);
S. Georgian
  • 143
  • 1
  • 2
  • 11
  • You have to do that inside the `res.on('end')` callback – michelem Dec 10 '16 at 09:11
  • that is how I ;ve done instead of console.log(temperature) I did return temperature and after at the end I did console.log(download(url)) and it printed undifined – S. Georgian Dec 10 '16 at 09:13
  • This type of question has literally been asked hundreds of times. You have to learn how to program with async results in Javascript. The duplicate above gives you many options for that. – jfriend00 Dec 10 '16 at 09:21

1 Answers1

0

So you need a callback to assign the variable to it and get from anywhere, look at cb:

var download = function(url, cb){
  //read the data\
  https.get(url,function(res){
    var data="";
    //add it to the data string
    res.on('data',function(chunk){
      data+=chunk;
    });
    //parse it with a callback function
    res.on('end',function(){
      var $=cheerio.load(data);
      var temperature=$("span.temp.swip").text();
      cb(temperature);
    });
  }).on('error',function(err){
    console.log(err.message)
  });
}

module.exports.download = download;

Then if you need to call the function from the web you need a route, use Express and save the previous file as download.js

var express = require('express'),
    app = express(),
    download = require('download.js');

app.get('/temperature', function (req, res) {
  // Get the temp
  var Ploiesti='44.9417,26.0237',
      Brasov='45.597,25.5525',
      url='https://darksky.net/forecast/' + Ploiesti + '/si24/en';

  download.download(url, function (temp) {
     // Send the response to the web
     res.json({ temperature: temp);
  });
});

app.listen(3000, function () {
  console.log('Example app listening on port 3000!')
});
michelem
  • 14,430
  • 5
  • 50
  • 66
  • Good answers should contain a text explanation for how you solved the OP's problem in addition to your code. This answer offers NO explanation and right now requires someone to do a visual diff between your code and the OP's code to try to figure out what you changed. – jfriend00 Dec 10 '16 at 09:19
  • yes I've done this and it works but I don't want to post the temperature on the console I want to save it to a variable or an object and after pass it and post it on a web page so I need to extract the variable temperature from that function – S. Georgian Dec 10 '16 at 09:19
  • If you need it in a web page you need to build up a route for node.js and download function to be called from the web. Then you can respond the call with the temperature. Look at Express to do this http://expressjs.com/en/starter/hello-world.html – michelem Dec 10 '16 at 09:23
  • Please look at edited answer I added everything you need. – michelem Dec 10 '16 at 09:30