Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

150
Views
Cant access items in object

I don't seem to be able to reach the contents of the object created. Here is my code:

function showPosition() {
    var geopos = new Object();
    if(navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(function(position) {
            geopos.lat= parseFloat(position.coords.latitude);
            geopos.long= parseFloat(position.coords.longitude);
            console.log(geopos['long']);// Displays the information fine
        });
    } else {
        geopos['error']= "Sorry, your browser does not support HTML5 geolocation.";
    }
    console.log(geopos); //Shows the object fine 
    console.log(geopos.lat); //undefined 
    return geopos;
}

As you can see by the comments i am getting undefined here. How can i access this information? Also tried with the other notation geopos['lat'] and works exactly the same. I've also tried with the answers from How can I display a JavaScript object? with the following outcome:

console.log(JSON.parse(JSON.stringify(geopos))); //empty object
console.log(JSON.stringify(geopos));//"{}"
console.log(Object.keys(geopos));//empty array
console.log(Object.values(geopos));//empty array

But if i output the entire object (with console.log(geopos)) i get the correct values in the console like this:

Object { } ​lat: 43.85249638386875 long: -9.65398821778 : Object { … }

How can i access the contents of this objects and why it isn't working?

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!