0

I know there are many questions about this issue but my problem is different I managed to get the data outside of $.getJSON() but I couldn't access them

 var country = {} ; 

     $.getJSON('https://freegeoip.net/json/', function (code) { 

        country.config = code ;

  });

console.log(country);

I will get these information

{}config: {ip: "**********", country_code: "LB", country_name: "Lebanon", region_code: "06", region_name: "Beirut", …}proto: Object

if I try to access them like this

country.config.country_code

I will get this error

fdgdfgd:643 Uncaught TypeError: Cannot read property 'country_code' of undefined

I can't even access this one

country.config

How can I get the data from this object ?

user3763103
  • 111
  • 3
  • 13
  • I changed the title I hope you read whole thing before mark it as duplicated. I managed to return the response I want to access it now – user3763103 Jan 13 '18 at 12:20
  • Your question is a 100% duplicate. Your code *cannot* work. You need to tackle and understand asynchronous calls. Your problem is not special or different. – deceze Jan 13 '18 at 12:23
  • But console.log printed the data outside getJSON function therefor it works fine, I got this solution from another answer and it's the simplest one. I just want to know how to access these data I can only print country I can't access further – user3763103 Jan 13 '18 at 12:27
  • No, read the other two duplicates as well. This. Can. Not. Work. – deceze Jan 13 '18 at 12:43
  • @deceze only the [1st](https://stackoverflow.com/q/14220321/673991) linked duplicate is the same issue. The other two are just confusing things. The 2nd seems to be about a browser bug and the 3rd is an unusual aspect of console.log() interactivity. – Bob Stein Jan 13 '18 at 14:47
  • @user3763103 because `console.log(country)` executes before `country.config = code`. – Bob Stein Jan 13 '18 at 14:47
  • @BobStein-VisiBone The other dupes pertain to why OP is seeing the behavior they do. – deceze Jan 13 '18 at 15:46
  • @deceze thank you, I see it now. Two time warps. – Bob Stein Jan 14 '18 at 02:11

0 Answers0