0

I get information about location, but can not use it in html.error => Uncaught (in promise) TypeError: Cannot set properties of undefined (setting 'geoLocationPlace').Why is the variable(geoLocationPlace)undefined?

 data() {
        return {
            inputValue:null,
            searchedPlace:null,
            geoLocationPlace:null,
        }
    },

          getLocation(){
            if(navigator.geolocation){
                navigator.geolocation.getCurrentPosition(function(position){
                    let long = position.coords.longitude;
                    let lat = position.coords.latitude;
                    
                    fetch(`https://api.bigdatacloud.net/data/reverse-geocode-client? 
                               latitude=${lat}&longitude=${long}&localityLanguage=en`)
                    .then(res => {
                        return res.json()
                    })
                    .then(resData => {
                        //resData.countryName return country
                        this.geoLocationPlace = resData.countryName
                        console.log(this.geoLocationPlace)
                    })
                    
                })
            
mchedlo14
  • 3
  • 3

1 Answers1

0

The issue is that this is "lost" because you use a regular function as the callback to navigator.geolocation.getCurrentPosition

You clearly understand the need for arrow functions, as your fetch chain uses them, so I can only conclude that you simply missed this function

getLocation(){
  if(navigator.geolocation){
    // fix the next line like so
    navigator.geolocation.getCurrentPosition((position) => {
      let long = position.coords.longitude;
Bravo
  • 6,022
  • 1
  • 10
  • 15