数据不断循环。我怎样才能让它只读一次?
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)
})
}
答案 0 :(得分:1)
我刚刚找到了答案。因此,我将其改为on()而不是ontouch。这是新代码。
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)})
});
});