1

I'm new to javascript and node.js and I have been really struggling to figure this scoping problem out. The code below makes a simple get request and displays the body of the webpage to the console - no problems there.

function makeGETRequest(host, path, headers) {
    var options = {
        hostname: host,
        port: 8000,
        path: path,
        method: 'GET',
        headers: headers
    };

    var request = http.request(options, function(response) {
        response.on('data', function(body) {
            console.log('Body: ' + body);
        });
    });

    request.end();
 }

What I can't seem to do is get any information out of the function declared inside http.request. If I want to save the body to a variable that I've declared in makeGETRequest() and then return it, it will be undefined because it goes out of scope. Creating a global variable and using that instead does not solve the problem.

ex.

function makeGETRequest(host, path, headers) {
        var siteBody;
        var options = {
        hostname: host,
        port: 8000,
        path: path,
        method: 'GET',
        headers: headers
    };

    var request = http.request(options, function(response) {
        response.on('data', function(body) {
            console.log('Body: ' + body);
            siteBody = body; //works as long as I don't exit the scope of the function
        });
    });

    request.end();
    return body; //returns undefined
 }

How do I get information body or otherwise out of these functions so that I can use the information in makeGETrequest() and then return it?

cOborski
  • 134
  • 1
  • 15
  • without the `var` keyword, the variable is globally scoped: http://www.w3schools.com/js/js_scope.asp : Automatically Global. However this does not ensure the function is finished. use a callback or promise to make sure the function has executed – online Thomas Jan 24 '17 at 22:26
  • it returns undefined because your `get` request is asynchronous, so your `return` statement will actually run before `siteBody = body;` – avrahamcool Jan 24 '17 at 22:27

1 Answers1

0

You will need to learn more about asynchronous Javascript function and how to solve them.

In asynchronous functions you just cannot assign the response to a variable and get it.

Instead you will need to use promise.

Pritam Banerjee
  • 17,953
  • 10
  • 93
  • 108
  • 2
    why the downvote? he is right. Please explain if you downvote – online Thomas Jan 24 '17 at 22:27
  • @Thomas Explanations are not required. If someone thinks an answer is not useful, they are entitled to that opinion. – 4castle Jan 24 '17 at 22:29
  • @Pritam To give a good quality answer. Please explain the problem and the solution. You have done neither. – 4castle Jan 24 '17 at 22:35
  • 2
    @4castle I said please, because I would like to know: as far as I know the answer is right, so if it is wrong I like to learn from this. – online Thomas Jan 24 '17 at 22:35
  • @4castle I guess, someone who is capable enough to do a post call is capable enough to learn about asynchronous functions, if mentioned about it. Also I mentioned that `promise` needs to be used to solve it. It's not always needed to spoon feed a person. And it is not mandatory to explain a downvote, but it definitely makes the person aware of his mistake if you explain the downvote and hence it is a good practice. – Pritam Banerjee Jan 24 '17 at 22:39
  • @PritamBanerjee Giving an explanation is not spoonfeeding. Any statements at all as to *why* what you're saying is true would suffice. Currently, your answer doesn't actually contain the answer. It's just recommending that they look for answers elsewhere. – 4castle Jan 24 '17 at 22:45
  • Oh thank you - really I just needed a push in the right direction. It's hard to solve a problem if you're not sure what to Google. – cOborski Jan 25 '17 at 14:12
  • @buguloo Can you please accept the answer if it helped you. – Pritam Banerjee Jan 25 '17 at 17:30