[英]Get initial value of Firestore document
我正在尝试在我的反应原生应用程序中添加杂货数量。 杂货来自 firebase 火库,具有预定义的价格,我希望当我增加数量时,应该正确计算 cart_items 的总数。 我通过直接从服务器添加它来解决这个问题。
问题是,我只需要能够获得杂货店的初始价格,所以我可以随意加减我需要再次增加数量时的当前价格。 我希望你明白我的意思。
const increment = async (id) => {
const itemRef = doc(db, "cartItems", id);
await getDoc(itemRef).then(async (snapshot) => {
// This Line of code is supposed to capture the initial value of the price
let price = snapshot.data().data.price;
console.log(price);
// This Line of code is supposed to capture the initial value of the price
await updateDoc(itemRef, {
quantity: snapshot.data().quantity + 1,
data: {
...snapshot.data().data,
price: snapshot.data().data.price + price,
// I am supposed to use that initial value for this calculation
},
});
});
};
这是为了减少数量
const decrement = async (id) => {
const itemRef = doc(db, "cartItems", id);
await getDoc(itemRef).then(async (snapshot) => {
// This Line of code is supposed to capture the initial value of the price
let price = snapshot.data().data.price;
console.log(price);
// This Line of code is supposed to capture the initial value of the price
await updateDoc(itemRef, {
quantity:
snapshot.data().quantity === 1 ? 1 : snapshot.data().quantity - 1,
data: {
...snapshot.data().data,
price:
snapshot.data().data.price === price
? price
: snapshot.data().data.price - price,
// I am supposed to use that initial value for this calculation
},
});
});
};
所以我只需要知道是否有一种方法可以只获取价格的初始值而不是更新的值。 如果我需要澄清有关该问题的任何内容,请告诉我。 这对我来说是一个非常紧迫的问题。
我得到了答案,伙计们。
我只需要添加一个保持不变且不会更改到我的数据库的初始值。 这就是我用来对我的应用程序进行必要计算的方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.