0

I would like to access 'key' which happens to be a global variable after being returned from this.dorequest(). I get the key inside the if block but can't access it anywhere outside. Can somebody please assist? Pretty sure it is scope/hoisting issue. Thanks in advance!

var key; 
jira.addNewIssue = function(issue) {
  var options = {
      uri: this.makeUri('/issue'),
      method: 'POST',
      followAllRedirects: true,
      json: true,
      body: issue
  };

  this.doRequest(options, function(error, response, body) {
      **var key;** (removed this but still an error)
      if (error) {
          console.log('Error1')
          return;
      }
      else {
          key = response.body.key;
          console.log("THIS IS RESPONSE BODY KEY:", key); //no errors 123
          return key;
      }
  });
};

console.log("hello key", key); //undefined
Mihir Patel
  • 2,202
  • 3
  • 23
  • 36
  • Possible duplicate of [How do I return the response from an asynchronous call?](http://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – Heretic Monkey Nov 01 '16 at 16:36

4 Answers4

3

You are using

var key;

inside the block which means you redefine it in the block scope which makes it a different variable within that scope. Just remove the var statement.

Plus you assign the key variable in an asynchronous call which might not have finished before you arrive at your console.log statement. Move your console.log statement into the callback function OR Create an event to trigger the console.log statement when callback has finished.

Probably not a good idea to use globals inside asynchronous callbacks. I am not sure whether your return statement does anything (depends on the API of the library you are using) but it would certainly not assign the key variable.

Falk Schuetzenmeister
  • 1,497
  • 1
  • 16
  • 35
  • I removed it. But do I need to use a return statement outside of if block?? hello key undefined App listening on port:8000 THIS IS RESPONSE BODY KEY: RES123 – Mihir Patel Nov 01 '16 at 16:28
  • 1
    There is another issue. You assign the value during an asynchronous call. That means the call might not have finished before you get to your console log statement. Move the console.log statement into your call back function. Not really a good idea to assign global variables in call back statements since you never know when they will change. Just for the fun of it you could also put your console.log statement into a indefinite loop. – Falk Schuetzenmeister Nov 01 '16 at 16:31
0

Please use window.key. All global JavaScript objects, functions, and variables automatically become members of the window object.

Mihai Alexandru-Ionut
  • 47,092
  • 13
  • 101
  • 128
0

Either wrap the code in a closure or add key to window.

i.e. window.key = undefined;

Craig Poole
  • 740
  • 8
  • 19
0

Either add code in a closure or make key variable global

i.e. window.key = undefined.

Also check value of response.body.key

Divyang Desai
  • 7,483
  • 13
  • 50
  • 76