【问题标题】:Firebase's update function lags behind button click on browserFirebase 的更新功能滞后于浏览器上的按钮点击
【发布时间】:2020-12-19 06:13:12
【问题描述】:

我正在尝试使用 firebase 在 Vue 中创建一个实时投票应用程序来存储投票数。我能够创建数据库并增加投票数,以及将来自数据库的最新投票显示到我的应用程序。但是,当我单击按钮将投票数更新为 1 时,投票仅在浏览器中增加,而不是在 firebase 上。

我是不是用的更新功能不对?

...methods:{
upVotePingu() {
      let pingu = db.collection('canidates').doc('pingu')

      // Atomically increment the population of the city by 50.
      pingu.set({
        votes: this.pingu.votes++
      })

      console.log('voted')
    }
...mounted(){
db.collection('canidates')
      .get()
      .then(query => {
        query.forEach(doc => {
          const dbVote = {
            vote: doc.data().votes
          }
          this.pingu.votes = dbVote.vote
          console.log(this.pingu.votes)
        })
      })
<button @click="upVotePingu">Vote</button>
data() {
    return {
      pingu: {
        votes: 0
      }
    }

【问题讨论】:

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


    【解决方案1】:

    你需要这样做:

      this.pingu.votes++
      pingu.set({
        votes: this.pingu.votes
      })
    

    另外,请注意,要增加字段的值,使用FieldValue.increment 更安全,请参阅https://firebase.googleblog.com/2019/03/increment-server-side-cloud-firestore.htmlhttps://firebase.google.com/docs/reference/js/firebase.firestore.FieldValue#static-increment

    【讨论】:

    • 嗨@TylerMorales,您有时间测试建议的答案吗?如果它可以解决您的问题,请接受它。谢谢。
    猜你喜欢
    • 2021-09-30
    • 2014-01-23
    • 2013-04-29
    • 1970-01-01
    • 2023-03-30
    • 1970-01-01
    • 2016-09-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多