Firesbase Firestore无法正常工作+ Vue.js

时间:2018-08-02 11:21:57

标签: javascript firebase vue.js google-cloud-firestore

我想将我的地理点存储到Firestore数据库中,但是它不起作用。 我创建了一个名为acctPosition的数组,用于在其中存储地理位置, 但是在firebase中我看不到数据,好像什么也没有存储。

如何正确存储地理位置?

输出:

output

Geoloaction组件:

    data () {
      coords: {
        latitude: null,
        longitude: null
      },
      acctPosition: []
    },
    ...

    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)
          var acctPos = self.acctPosition.push(pos)
          console.log(acctPos)
        }
      } else {
        // Browser doesn't support Geolocation
      }

      let docId = `${this.currentUser.uid}`
      // store geoData to currentUser in firebase firestore
      fb.usersCollection.doc(docId).set({
        acctPosition: this.acctPos
      }).then(ref => {
        console.log('work')
      }).catch(err => {
        console.log(err)
      })
    }

1 个答案:

答案 0 :(得分:0)

JavaScript是异步的,因此fb.usersCollection.doc(docId).set()将在getCurrentPosition完成之前执行。

完成getCurrentPosition后,您可以推送到Firebase:

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
      }
}