【问题标题】:Firesbase firestore not working + VuejsFirebase Firestore 不工作 + Vue Js
【发布时间】:2018-08-02 11:21:56
【问题描述】:

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

如何正确存储地理点?

输出:

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

【问题讨论】:

  • 我认为这个错误很简单。您可以在尝试保存数据之前执行console.log(this.acctPos) 吗?
  • 一切都很好,但感谢您的帮助

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


【解决方案1】:

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

【讨论】:

  • 转普通字符串?
猜你喜欢
  • 2021-05-08
  • 1970-01-01
  • 2018-05-28
  • 1970-01-01
  • 2021-05-26
  • 2018-12-02
  • 2023-02-21
  • 2018-03-18
  • 1970-01-01
相关资源
最近更新 更多