Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

158
Visualizações
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
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda