Firebase不以我期望的速度通过JavaScript更新数值。

huangapple go评论75阅读模式
英文:

Firebase not updating values as fast as I want with Javascript

问题

[![在此输入图像描述][1]][1]

  [1]: https://i.stack.imgur.com/j5L3g.png

所以我有这个网站,它像一个统计表,当我按下-1按钮时,游戏的得分减少1分。我使用JavaScript访问Firebase数据库,然后将得分减1。

const docRef = doc(db, "Games", game_id);
const docSnap = await getDoc(docRef);
var score = docSnap.data().team2_score
await updateDoc(docRef, {
team2_score: score -= number
});


问题是,如果用户快速多次点击它,那么Firebase不会执行所有这些操作。所以如果用户快速点击按钮5次,Firebase数据库只会跟踪其中4次。这是我的问题。

是否有一种方法可以确保数据库中的每次点击都会更新,即使点击得很快?
英文:

Firebase不以我期望的速度通过JavaScript更新数值。

So I have this website, that acts like a stat sheet and when I press the -1 button, the score of the game goes down by 1. I'm using javascript to access the firebase database and then subtract the score by 1.

const docRef = doc(db, "Games", game_id);
const docSnap = await getDoc(docRef);
var score = docSnap.data().team2_score
await updateDoc(docRef, {
  team2_score: score -= number
});

The issue is that if a user clicks it really fast multiple times, then Firebase doesn't do all those operations. So if a user clicks the button 5 times really fast, the Firebase database would only keep track of 4 of them. This is my issue.

Is there a way to make sure that every single one of those clicks updated in the database, even when clicked really fast?

答案1

得分: 2

Option 1
使用 Firebase Increment:https://cloud.google.com/firestore/docs/samples/firestore-data-set-numeric-increment

const docRef = doc(db, "Games", game_id);
await updateDoc(docRef, {
  team2_score: firebase.firestore.FieldValue.increment(-1)
});

Option 2
使用事务。https://firebase.google.com/docs/firestore/manage-data/transactions#transactions

const docRef = doc(db, "Games", game_id);

try { 
const result = await db.runTransaction((transaction) => {
    // 如果存在冲突,此代码可能会被多次运行。
    return transaction.get(docRef).then((sfDoc) => {
        if (!sfDoc.exists) {
            throw "文档不存在!";
        }

        // 注意:可以使用 FieldValue.increment() 更新分数,而不需要事务
        var newScore = sfDoc.data().team2_score - 1;
        transaction.update(docRef, { team2_score: newScore });
        });
    })

    console.log("事务成功提交!");

} catch (error)  {

    console.log("事务失败:", error);

}
英文:

You have two options:

Option 1

Use Firebase Increment: https://cloud.google.com/firestore/docs/samples/firestore-data-set-numeric-increment

const docRef = doc(db, "Games", game_id);
await updateDoc(docRef, {
  team2_score: firebase.firestore.FieldValue.increment(-1)
});

Option 2

Use a Transaction. https://firebase.google.com/docs/firestore/manage-data/transactions#transactions

const docRef = doc(db, "Games", game_id);

try { 
const result = await db.runTransaction((transaction) => {
    // This code may get re-run multiple times if there are conflicts.
    return transaction.get(docRef).then((sfDoc) => {
        if (!sfDoc.exists) {
            throw "Document does not exist!";
        }

        // Note: this could be done without a transaction
        //       by updating the score using FieldValue.increment()
        var newScore= sfDoc.data().team2_score - 1;
        transaction.update(docRef, { team2_score: newScore});
        });
    })

    console.log("Transaction successfully committed!");

} catch(error)  {

    console.log("Transaction failed: ", error);

}

huangapple
  • 本文由 发表于 2023年2月10日 05:24:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/75404554.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定