【问题标题】:How to increment a data field in Firestore using Firebase Modular SDK V9?如何使用 Firebase Modular SDK V9 增加 Firestore 中的数据字段?
【发布时间】:2021-08-31 15:12:57
【问题描述】:

所以我正在尝试增加数据以将 num 发送到其他用户的 num。

const sendNum = async(e) => {
        e.preventDefault();
        
        
        const targetQuery = query(userCol, where("keycode", "==", parseInt(target)));
        const targetSnapshot = await getDocs(targetQuery)
        
        targetSnapshot.forEach((document) => {
            console.log(document.data().num)
            console.log(document.id)
            const targetRef = doc(db, 'users', document.id)

            setDoc(targetRef, {
                num: // increment user data here
            }, {merge: true})
        })
}

但是我发现的一个文档,我并不真正理解它们。 而据我所知,它已经过时了。 所以如果有人知道怎么做,我会很高兴:)

【问题讨论】:

    标签: javascript firebase google-cloud-firestore


    【解决方案1】:

    该文档有一个专门的部分,称为increment a numeric value

    import { setDoc, increment } from "firebase/firestore";
    
    await setDoc(targetRef, {
        num: increment(50)
    });
    

    您不能在forEach 循环中使用awaitsetDoc 返回一个承诺并且必须等待)所以请尝试使用Batch WritesPromise.all()

    Using async/await with a forEach loop

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-11-01
      • 2022-06-11
      • 2021-11-10
      • 2022-08-20
      • 2023-03-23
      • 2020-07-26
      • 2018-11-18
      相关资源
      最近更新 更多