Javascript Firestore获取数据一次,然后更新。数据快照的循环

Javascript Firestore获取数据一次,然后更新。数据快照的循环,javascript,reactjs,google-cloud-firestore,Javascript,Reactjs,Google Cloud Firestore,数据不断循环。我怎样才能让它只读一次 onAddToCart() { const { id, product, authUser, cart, shippingType } = this.state; this.props.firebase.cartItems().doc(authUser.uid).collection('total').doc('total').onSnapshot(snapshot => { this.props.fir

数据不断循环。我怎样才能让它只读一次

 onAddToCart() {
        const { id, product, authUser, cart, shippingType } = this.state;

  this.props.firebase.cartItems().doc(authUser.uid).collection('total').doc('total').onSnapshot(snapshot => {
            this.props.firebase.cartItems().doc(authUser.uid).collection("total").doc("total").update({'total': snapshot.data().total + (product.salePrice > 0 ? product.salePrice : product.price * 1)
        }).then(() => {

        cogoToast.success("Added to store list");

    });

                console.log("doc info",snapshot.data().total)



        })

    }

我刚刚找到了答案。因此,我将它改为get(),而不是onsnapchat。这是新代码

this.props.firebase.cartItems().doc(authUser.uid).collection('total').get().then(snap => {
    snap.forEach(doc => {
        console.log(doc.data());
        this.props.firebase.cartItems().doc(authUser.uid).collection("total").doc("total").update({'total': doc.data().total + (product.salePrice > 0 ? product.salePrice : product.price * 1)})

    });
});

哦,很好我也在准备回答+1.