Saving body from request.get() [duplicate]Why is my variable unaltered after I modify it inside of a function? - Asynchronous code referenceHow do I return the response from an asynchronous call?How do I remove a property from a JavaScript object?Remove empty elements from an array in JavascriptGet selected text from a drop-down list (select box) using jQueryHow do I remove a particular element from an array in JavaScript?How do I completely uninstall Node.js, and reinstall from beginning (Mac OS X)Writing a JSON file after a lot of async requestsHow do I return the response from an asynchronous call?What is the --save option for npm install?Request data to global variableWait for data from external API before making POST request

Solenoid fastest possible release - for how long should reversed polarity be applied?

"Counterexample" for the Inverse function theorem

Polynomial division: Is this trick obvious?

Would a "ring language" be possible?

How can I fix the label locations on my tikzcd diagram?

How can we delete item permanently without storing in Recycle Bin?

What formula to chose a nonlinear formula?

Given 0s on Assignments with suspected and dismissed cheating?

Do we see some Unsullied doing this in S08E05?

Promotion comes with unexpected 24/7/365 on-call

Cannot remove door knob -- totally inaccessible!

Would it be fair to use 1d30 (instead of rolling 2d20 and taking the higher die) for advantage rolls?

I recently started my machine learning PhD and I have absolutely no idea what I'm doing

How come Arya Stark didn't burn in Game of Thrones Season 8 Episode 5

How could it be that 80% of townspeople were farmers during the Edo period in Japan?

Why would you put your input amplifier in front of your filtering for and ECG signal?

Is there any deeper thematic meaning to the white horse that Arya finds in The Bells (S08E05)?

What would a Dragon have to exhale to cause rain?

How was the blinking terminal cursor invented?

When the match time is called, does the current turn end immediately?

How does the Heat Metal spell interact with a follow-up Frostbite spell?

When did Britain learn about American independence?

Why is so much ransomware breakable?

Failing students when it might cause them economic ruin



Saving body from request.get() [duplicate]


Why is my variable unaltered after I modify it inside of a function? - Asynchronous code referenceHow do I return the response from an asynchronous call?How do I remove a property from a JavaScript object?Remove empty elements from an array in JavascriptGet selected text from a drop-down list (select box) using jQueryHow do I remove a particular element from an array in JavaScript?How do I completely uninstall Node.js, and reinstall from beginning (Mac OS X)Writing a JSON file after a lot of async requestsHow do I return the response from an asynchronous call?What is the --save option for npm install?Request data to global variableWait for data from external API before making POST request






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








0
















This question already has an answer here:



  • Why is my variable unaltered after I modify it inside of a function? - Asynchronous code reference

    6 answers



I want to save the body from a get request in a variable (using the request module), but its undefined.



I've searched for a solution but nothing worked so far. Guess the problem is that request.get is calles async (so far i have no experience in async functions).



const request = require("request")
...
let foo;
request.get(options,function(err,response,body)
foo = body;
);
console.log(foo);


expected: the body of the valid get-request
actual: undefined










share|improve this question













marked as duplicate by Jonathan Lonowski javascript
Users with the  javascript badge can single-handedly close javascript questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Mar 23 at 16:45


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
























    0
















    This question already has an answer here:



    • Why is my variable unaltered after I modify it inside of a function? - Asynchronous code reference

      6 answers



    I want to save the body from a get request in a variable (using the request module), but its undefined.



    I've searched for a solution but nothing worked so far. Guess the problem is that request.get is calles async (so far i have no experience in async functions).



    const request = require("request")
    ...
    let foo;
    request.get(options,function(err,response,body)
    foo = body;
    );
    console.log(foo);


    expected: the body of the valid get-request
    actual: undefined










    share|improve this question













    marked as duplicate by Jonathan Lonowski javascript
    Users with the  javascript badge can single-handedly close javascript questions as duplicates and reopen them as needed.

    StackExchange.ready(function()
    if (StackExchange.options.isMobile) return;

    $('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
    var $hover = $(this).addClass('hover-bound'),
    $msg = $hover.siblings('.dupe-hammer-message');

    $hover.hover(
    function()
    $hover.showInfoMessage('',
    messageElement: $msg.clone().show(),
    transient: false,
    position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
    dismissable: false,
    relativeToBody: true
    );
    ,
    function()
    StackExchange.helpers.removeMessages();

    );
    );
    );
    Mar 23 at 16:45


    This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.




















      0












      0








      0









      This question already has an answer here:



      • Why is my variable unaltered after I modify it inside of a function? - Asynchronous code reference

        6 answers



      I want to save the body from a get request in a variable (using the request module), but its undefined.



      I've searched for a solution but nothing worked so far. Guess the problem is that request.get is calles async (so far i have no experience in async functions).



      const request = require("request")
      ...
      let foo;
      request.get(options,function(err,response,body)
      foo = body;
      );
      console.log(foo);


      expected: the body of the valid get-request
      actual: undefined










      share|improve this question















      This question already has an answer here:



      • Why is my variable unaltered after I modify it inside of a function? - Asynchronous code reference

        6 answers



      I want to save the body from a get request in a variable (using the request module), but its undefined.



      I've searched for a solution but nothing worked so far. Guess the problem is that request.get is calles async (so far i have no experience in async functions).



      const request = require("request")
      ...
      let foo;
      request.get(options,function(err,response,body)
      foo = body;
      );
      console.log(foo);


      expected: the body of the valid get-request
      actual: undefined





      This question already has an answer here:



      • Why is my variable unaltered after I modify it inside of a function? - Asynchronous code reference

        6 answers







      javascript node.js node-request






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Mar 23 at 16:37









      DrSteifDrSteif

      1




      1




      marked as duplicate by Jonathan Lonowski javascript
      Users with the  javascript badge can single-handedly close javascript questions as duplicates and reopen them as needed.

      StackExchange.ready(function()
      if (StackExchange.options.isMobile) return;

      $('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
      var $hover = $(this).addClass('hover-bound'),
      $msg = $hover.siblings('.dupe-hammer-message');

      $hover.hover(
      function()
      $hover.showInfoMessage('',
      messageElement: $msg.clone().show(),
      transient: false,
      position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
      dismissable: false,
      relativeToBody: true
      );
      ,
      function()
      StackExchange.helpers.removeMessages();

      );
      );
      );
      Mar 23 at 16:45


      This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









      marked as duplicate by Jonathan Lonowski javascript
      Users with the  javascript badge can single-handedly close javascript questions as duplicates and reopen them as needed.

      StackExchange.ready(function()
      if (StackExchange.options.isMobile) return;

      $('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
      var $hover = $(this).addClass('hover-bound'),
      $msg = $hover.siblings('.dupe-hammer-message');

      $hover.hover(
      function()
      $hover.showInfoMessage('',
      messageElement: $msg.clone().show(),
      transient: false,
      position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
      dismissable: false,
      relativeToBody: true
      );
      ,
      function()
      StackExchange.helpers.removeMessages();

      );
      );
      );
      Mar 23 at 16:45


      This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
























          1 Answer
          1






          active

          oldest

          votes


















          0














          The request is done in asynchronous way so your console.log happens before the request is completed, so foo is still undefined.



          You must put your console log inside the callback body. Also, any computation requiring the result from the request should be started from within the callback.



          request.get(options,function(err,response,body)
          console.log(body);
          doSomethingAfterMyRequest(body)
          );


          Also, you should handle your error, because if an error occurs during your request, it's possible that body remains undefined.



          request.get(options,function(err,response,body)
          if (err)
          console.error(err)
          return

          console.log(body);
          doSomethingAfterMyRequest(body)
          );





          share|improve this answer





























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            The request is done in asynchronous way so your console.log happens before the request is completed, so foo is still undefined.



            You must put your console log inside the callback body. Also, any computation requiring the result from the request should be started from within the callback.



            request.get(options,function(err,response,body)
            console.log(body);
            doSomethingAfterMyRequest(body)
            );


            Also, you should handle your error, because if an error occurs during your request, it's possible that body remains undefined.



            request.get(options,function(err,response,body)
            if (err)
            console.error(err)
            return

            console.log(body);
            doSomethingAfterMyRequest(body)
            );





            share|improve this answer



























              0














              The request is done in asynchronous way so your console.log happens before the request is completed, so foo is still undefined.



              You must put your console log inside the callback body. Also, any computation requiring the result from the request should be started from within the callback.



              request.get(options,function(err,response,body)
              console.log(body);
              doSomethingAfterMyRequest(body)
              );


              Also, you should handle your error, because if an error occurs during your request, it's possible that body remains undefined.



              request.get(options,function(err,response,body)
              if (err)
              console.error(err)
              return

              console.log(body);
              doSomethingAfterMyRequest(body)
              );





              share|improve this answer

























                0












                0








                0







                The request is done in asynchronous way so your console.log happens before the request is completed, so foo is still undefined.



                You must put your console log inside the callback body. Also, any computation requiring the result from the request should be started from within the callback.



                request.get(options,function(err,response,body)
                console.log(body);
                doSomethingAfterMyRequest(body)
                );


                Also, you should handle your error, because if an error occurs during your request, it's possible that body remains undefined.



                request.get(options,function(err,response,body)
                if (err)
                console.error(err)
                return

                console.log(body);
                doSomethingAfterMyRequest(body)
                );





                share|improve this answer













                The request is done in asynchronous way so your console.log happens before the request is completed, so foo is still undefined.



                You must put your console log inside the callback body. Also, any computation requiring the result from the request should be started from within the callback.



                request.get(options,function(err,response,body)
                console.log(body);
                doSomethingAfterMyRequest(body)
                );


                Also, you should handle your error, because if an error occurs during your request, it's possible that body remains undefined.



                request.get(options,function(err,response,body)
                if (err)
                console.error(err)
                return

                console.log(body);
                doSomethingAfterMyRequest(body)
                );






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Mar 23 at 16:44









                François P.François P.

                2,115716




                2,115716















                    Popular posts from this blog

                    Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

                    Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

                    Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript