title: Unable to access response object data outside the function in node js
body: <p>I am using node js and making a call to spotify API and receive the response in body object, as shown in below code:</p>
  
  <pre><code>    var options = {
            url: 'https://api.spotify.com/v1/me',
            headers: { 'Authorization': 'Bearer ' + access_token },
            json: true
      };
      request.get(options, function(error, res, body) {
            console.log(body)
      });
  </code></pre>
  
  <p>This gives me output:
  <a href="https://i.stack.imgur.com/sZGj8.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/sZGj8.jpg" alt="enter image description here"></a></p>
  
  <p>But now when I try to access the body object outside the function I get undefined. I think the problem is that I am making an asynchronous call and so before the response is received the statements where I make use of body variable outside function are executed. But I am a bit confused about how to get to the solution.</p>
  
  <p>Any help is appreciated</p>
  
  <p><strong>Edit:</strong></p>
  
  <pre><code>    request.get(options, function(error, res, body) {
          console.log(body)
          response.render('user_account.html', {
                  data: body
          })
      });
  </code></pre>
  
  <p>And it gives the output:</p>
  
  <p><a href="https://i.stack.imgur.com/40rOU.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/40rOU.jpg" alt="enter image description here"></a></p>