[Vuejs]-Geolocation with vuejs and firestore

0👍

this inside is not Vue instance. You can do a trick by assign self = this or using arrow function:

   mounted () {
    ...
     var self = this;
    // Try HTML5 geolocation.
      if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(function(position) {
          var pos = {
            lat: position.coords.latitude,
            lng: position.coords.longitude
          };
          console.log(pos)
          map.setCenter(pos);
          var acctPos = self.acctPosition.push(pos)
          console.log(acctPos)

          let docId = `${self.currentUser.uid}`

          // store geoData to currentUser in firebase firestore
          fb.usersCollection.doc(docId).set({
            acctPosition: self.acctPosition
          }).then(ref => {
            console.log('work')
          }).catch(err => {
            console.log(err)
          })
        }
      } else {
        // Browser doesn't support Geolocation
      }
}
👤ittus

Leave a comment