1

I'm trying to initializing body of request function to a global variable but useless. It's returning blank output. Kindly help me to access the body outside the function.

code image

Yousaf
  • 27,861
  • 6
  • 44
  • 69
Bilal Ahmad
  • 75
  • 1
  • 7
  • could you show us some code ? the result expected ? and what you get ? – Thibaud Aug 04 '21 at 10:15
  • there's about link of code image. you can check – Bilal Ahmad Aug 04 '21 at 10:17
  • 1
    The last console.log statement will execute _before_ the callback function of `request` is called. – Yousaf Aug 04 '21 at 10:17
  • console.log is working perfectly inside the request function but not outside. – Bilal Ahmad Aug 04 '21 at 10:20
  • @BilalSheikh avoid using image on stack overflow, use 3x` to show us code – Thibaud Aug 04 '21 at 10:20
  • If you're relying on global state you're probably doing something wrong. Just do what you want to do inside the request. – szatkus Aug 04 '21 at 10:21
  • _"console.log is working perfectly inside the request function but not outside"_ - you didn't get an idea of why that is from [this comment](https://stackoverflow.com/questions/68649434/using-request-response-as-a-global-variable#comment121322123_68649434)? Callback function of `request` is invoked asynchronously, i.e. _after_ your script has finished executing. `console.log` statement at the end of your code is executed _before_ the callback function initializes `gdata`. – Yousaf Aug 04 '21 at 10:22
  • @Yousaf then how can I wait until call back ? – Bilal Ahmad Aug 04 '21 at 10:24
  • Move the `console.log` inside the callback function. Do what you want to do with `gdata` inside the callback function. Even better approach is to directly use `body` and get rid of `gdata`. See the link to another stackoverflow question linked in one of my earlier comments. You can't do what you are trying to do - at-least not in a reliable way. – Yousaf Aug 04 '21 at 10:26
  • but i want to store this to another variable to use it later – Bilal Ahmad Aug 04 '21 at 10:29
  • Use it where? What do you want to do with `gdata`? – Yousaf Aug 04 '21 at 10:31
  • I want to get **body** from request function and assign it to **gdata** – Bilal Ahmad Aug 04 '21 at 10:34
  • I can see that. My question is what do you want to do with `gdata`? – Yousaf Aug 04 '21 at 10:37
  • body will return json data. I will store it to gdata variable. then i will use parse json data to js object. after this, i will use this variable to show data on browser in an organized way. – Bilal Ahmad Aug 04 '21 at 10:40
  • You can do all that inside the callback function or create a function and call that function from inside of the callback function. – Yousaf Aug 04 '21 at 10:42
  • I'm yet confused but thanks dear <3 – Bilal Ahmad Aug 04 '21 at 10:45
  • Just do with the `body` variable what you want intended to do with `gdata`. Forget `gdata` and the last console.log statement. Inside the callback function, parse the json and display it in the browser. – Yousaf Aug 04 '21 at 10:50
  • okay dear, I got it – Bilal Ahmad Aug 04 '21 at 10:56

0 Answers0