0

I'm very new to JS and I'm trying to set the value of a variable within a function but it is 'undefined' after setting the value. My code is as follows:

    (function (name, context, definition){ 'use strict' ...} ('FP2', this, function (){...})
var fp = new FP2();   
fp.get(function(result, components) { for (var index in components){...};
    var IP = 'nothing';                                                               
    $.getJSON('http://gd.geobytes.com/GetCityDetails?callback=?', function(data) {
      this.IP = JSON.stringify(data, null, 2);
      alert(this.IP);
    });

    alert(IP);
});

I expect that the IP variable have the same value inside and outside the function, but it does not. The inner alert shows the expected value but the alert outside the function shows 'nothing'. From similar questions 1, 2, 3, I'm not using var to redeclare a local variable inside the function, and I'm pointing to the IP variable outside the function using 'this.', so I don't understand why it does not work as expected?

Alex
  • 1,914
  • 6
  • 26
  • 47

2 Answers2

1

No need to use this.IP, when you use this inside the API call, you are referring to that API call function itself. Just using IP should work.

The alert(IP); outside the API call wouldn't return the result as well. This function call will be immediately called even before the API returns some data so it will print out the previous value undefined.

(function (name, context, definition){ 'use strict' ...} ('FP2', this, function (){...})
var fp = new FP2();   
fp.get(function(result, components) { for (var index in components){...};
    var IP = undefined;                                                               
    $.getJSON('http://gd.geobytes.com/GetCityDetails?callback=?', function(data) {
     IP = JSON.stringify(data, null, 2);
     alert(IP);
    });

    alert(IP); //This will return undefined
});
  • My goal is to have the IP value outside of the function, so I want the second alert shows what I have set within the function. – Alex Sep 09 '18 at 13:14
  • Can you explain more, I don't get you. How can it be fixed? – Alex Sep 09 '18 at 13:29
  • You need to wait for the API to return any result before using it. so the solution is either move all the code you need to run after getting result inside that function or use Promise explicitly. – Jassim Abdul Latheef Sep 09 '18 at 13:31
0

If you want get IP value at anywhere, use follow method:

var IP = undefined;
var fpData = {};
function setIP(){
  var valueIP = undefined;
  $.ajax({
        url: "http://gd.geobytes.com/GetCityDetails?callback=?",
        async: false,
        dataType: 'json',
        success: function(data) {
            valueIP = JSON.stringify(data, null, 2); //console.log(valueIP);
            //return valueIP;
            localStorage.setItem('IP', valueIP);
        }
    });
  //return valueIP;
}

//call setIP
setIP();
IP = localStorage.getItem('IP');
console.log(IP);
protoproto
  • 2,081
  • 1
  • 13
  • 13