从Firebase获取一个值,并使用当前值+1更新它 [英] Get a value from Firebase and update it with the current value + 1
问题描述
<这是当前的代码:
$ b $ pre $ 导出函数updateQTY(条形码){
database.ref('items /'+条形码).update({
qty:currentQTY(条形码)
})
导出函数currentQTY(条形码){
database.ref(' ('value')。然后(function(snapshot){
var qty = snapshot.val()。qty
console.log(qty)
} )
return qty + 1
}
基本上,我想要的是将qty + 1返回给函数 updateQTY
。
Maby我必须做一个完全不同的方法,但是这很好,我只是不明白如何去做。我知道实际得到当前数量的函数必须返回它,但是我不明白如何在其他函数中捕获它。解决方案
你不能返回一个不可用的值,所以 currentQTY
需要返回一个 promise <新的数量;然后在 updateQTY
中,等待该承诺完成,然后然后更新数据。
导出函数updateQTY(barcode){
return currentQTY(barcode)
.then(qty => database.ref('items /'+ barcode)。更新({数量}));
)
导出函数currentQTY(barcode):Promise< number> {
return database.ref('items /'+ barcode).once('value')
.then(snapshot => snapshot.val()。qty + 1);
如果您可以使用async / await:
导出异步函数updateQTY(barcode){
const qty = await currentQTY(barcode);
database.ref('items /'+ barcode).update({qty}));
)
甚至
<$ (条形码){
database.ref('items /'+ barcode).update({qty:await currentQTY(barcode)}); pre> export async function updateQTY
)
但是,您可以通过事务更轻松,更强大地实现这一点:
导出函数updateQTY(barcode){
return database.ref('items /'+ barcode).transaction(data => ; {
data.qty ++;
返回数据;
});
$ / code>
或者如果您愿意的话
导出函数updateQTY(barcode){
return database.ref(`items / {$ barcode} / qty`).transaction(qty => qty ++);
$ / code>
使用事务的好处是能够正确处理多个用户试图同时更新数据。有关详情,请参阅文档。 p>
So yeah the title has the question. A little information this is what my Firebase database looks like:
And this is the current code:
export function updateQTY(barcode) {
database.ref('items/' + barcode).update({
qty: currentQTY(barcode)
})
}
export function currentQTY(barcode) {
database.ref('items/' + barcode).once('value').then(function(snapshot) {
var qty = snapshot.val().qty
console.log(qty)
})
return qty + 1
}
Basically, What I want is for the qty + 1 to be returned to the function updateQTY
.
Maby I have to do a completely different way but that's fine, I just don't understand how to do it. I understand the fact that the function that actually gets the current qty has to return it, but then I don't understand how to catch it in the other function.
You can't return a value which won't be available right away, so currentQTY
needs to return a promise for the new quantity; then, in updateQTY
, wait for that promise to fulfill and then update the data.
export function updateQTY(barcode) {
return currentQTY(barcode)
.then(qty => database.ref('items/' + barcode).update({qty}));
)
export function currentQTY(barcode): Promise<number> {
return database.ref('items/' + barcode).once('value')
.then(snapshot => snapshot.val().qty + 1);
}
If you can use async/await:
export async function updateQTY(barcode) {
const qty = await currentQTY(barcode);
database.ref('items/' + barcode).update({qty}));
)
Or even
export async function updateQTY(barcode) {
database.ref('items/' + barcode).update({qty: await currentQTY(barcode)});
)
However, you could accomplish this more easily and robustly with transactions:
export function updateQTY(barcode) {
return database.ref('items/' + barcode).transaction(data => {
data.qty++;
return data;
});
}
Or if you prefer
export function updateQTY(barcode) {
return database.ref(`items/{$barcode}/qty`).transaction(qty => qty++);
}
Using transactions has the advantage that it will correctly handle situations in which multiple users are trying to update the data at the same time. For more information, see the documentation.
这篇关于从Firebase获取一个值,并使用当前值+1更新它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!