Return JSON from Javascript function, but undefined

Viewed 19

I have this code:

 const options = {
      enableHighAccuracy: true,
      timeout: 5000,
      maximumAge: 0,
    };
    
    function success(pos) {
      const crd = pos.coords;
    
      var crd_json = {
        latitude: crd.latitude,
        longitude: crd.longitude,
        accuracy: crd.accuracy,
      };
    
      return crd_json;
    }
    
    function error(err) {

      var err_json = {
        code: err.code,
        msg: err.msg,
      };
    
      return err_json;
    }
    
    function getLocation() {
      navigator.geolocation.getCurrentPosition(success, error, options);
    }

And i need that when i call the function getLocation() returns me the JSON but when i call it in a variable like this:

const location = getLocation()
console.log(location)

Returns me a undefined

What i am doing wrong?

0 Answers
Related