1
var fs = require('fs');
var request = require('request');
var cheerio = require('cheerio');

var link = "www.google.com";

request(link, function (error, response, html) {

    if (!error && response.statusCode == 200) {

        var $ = cheerio.load(html);

        //scrape article
        $('.someclass').filter(function () {

            var data = $(this);
            var description = data.html();

            //write data to file
            fs.appendFile('description.txt', description + "\n", function (err) {
                if (err)
                    throw err;
                //console.log('The "description" was appended to file!');
            });

        })

    }
});

I am using this code in my node.js to get some data. But I want to use the variable description globally (outside of the request procedure). I've tried with return and it didn't work. How to do this?

Edit: This is another question that I asked. This question and question above are giving me same problems. Solving one of them or both will help a lot.

node.js multiple functions within request

Community
  • 1
  • 1
edinvnode
  • 3,497
  • 7
  • 30
  • 53
  • 1
    @ArmandGrillet well it's similar, but that question is about browser JavaScript and this is a Node issue. – Pointy May 27 '15 at 16:06
  • 1
    The `request()` call is **asynchronous**. Even if you make `description` relatively global, you won't be able to use the values it takes on until the callback is invoked later. Returning values from asynchronous operations fundamentally doesn't make sense. – Pointy May 27 '15 at 16:14
  • 1
    Sorry I sent the wrong link. This is a possible duplicate of [this question](http://stackoverflow.com/questions/2788159/turn-javascript-local-variable-into-global-variable). – Armand Grillet May 27 '15 at 16:14
  • 1
    I'd strongly suggest you read this answer: [How to return the response from an asynchronous call?](http://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-asynchronous-call/14220323#14220323). – jfriend00 May 27 '15 at 16:18
  • @ArmandGrillet - this is not a duplicate of that question at all. This is an async issue and is not in the browser either (e.g. no `window` object). – jfriend00 May 27 '15 at 16:20
  • Solved. I used some examples from other answers and got what I wanted. – edinvnode May 28 '15 at 04:53
  • Set a global variable like http://stackoverflow.com/questions/5447771/node-js-global-variables Suppose your declared variable is global.myVar, you can set this global.myVar from the callback and use the variable after the callback has returned. – user2975123 Jun 10 '15 at 03:36

1 Answers1

0

Would placing the description variable outside the request function with a dummy value work? Such as:

var fs = require('fs');
var request = require('request');
var cheerio = require('cheerio');

var link = "www.google.com";
var description="placeholder";
request(link, function (error, response, html) {
...

            description = data.html();
...
}
  • Usually, this will not work for an asynchronous operation which is what `request()` is. – jfriend00 May 27 '15 at 16:20
  • This is another way of solving same problem. http://stackoverflow.com/questions/30489087/node-js-multiple-functions-within-request/30489246?noredirect=1#comment49057000_30489246 – edinvnode May 27 '15 at 17:38