0
  var request = require("request");
  var path = "xxx.json";
  getBody : function(callback, path) {
      var self = this;
      var options = {
        method : "GET",
        uri: path,
        headers: {
          'authorization-type': 'application/json',
          'Connection':    'keep-alive',
          'Accept':        '*/*',
         },
        json: true,
      };

      request.get(options, function(error, response, body){
        self.key = body.key;  
        console.log(self.key);
      });
     console.log(self.key);
    }

The second console.log give always an undefined, How can I read the content of body when I'm out of the request.get? And I've tried with fucntion callback and also global variable, none of them works, thanks in advance for yours suggestions.

Lu LI
  • 21
  • 1
  • you can use a global variable and then assign into it inside the request.get method. Then you can call that global from the outside. This is to do with the scope of a variable – HexaCrop Oct 19 '17 at 10:10
  • 2
    @KevinRED — That is, more or less, what they are trying to do. It isn't wokring because they are trying to read the value before they set it. Using a global variable won't change the order of events. – Quentin Oct 19 '17 at 10:12
  • @Quentin, forgot that, thanks – HexaCrop Oct 19 '17 at 10:13

0 Answers0