如何在firebase中使用promise'.then'事务组件(how to use the promise '.then' component of tra

编程入门 行业动态 更新时间:2024-10-23 20:29:44
如何在firebase中使用promise'.then'事务组件(how to use the promise '.then' component of transaction in firebase)

我使用的是Firebase,需要增加一个柜台,所以transaction似乎是最合适的竞争者。 我明白,交易使用承诺,但我不清楚(和手册没有例子)如何使用承诺的.then组件。

目前在原型设计中,我可以通过使用像这样的警告来“放慢速度”:

var oldPosScore = firebase.database().ref('/data/n:'+stateNum[plus] + '/score/'); oldPosScore.transaction (function(increment1) { alert ('POSITIVE statement number: ' + stateNum[plus] ); return ((increment1 || 0)*1 + 1); });

我用什么语法来设置它? 那么这个用例在那里/如何工作?

编辑 (添加数据结构):我使用数字,而不是火力点按键,所以我可以随机选择它们。

I'm using firebase and need to increment a counter so transaction seems to be the most appropriate contender. i understand that transaction uses a promise, however i am unclear (and the manuals dont have examples) on how to use the .then component of the promise.

At the moment in prototyping, i can 'slow it down' by using an alert like this:

var oldPosScore = firebase.database().ref('/data/n:'+stateNum[plus] + '/score/'); oldPosScore.transaction (function(increment1) { alert ('POSITIVE statement number: ' + stateNum[plus] ); return ((increment1 || 0)*1 + 1); });

What syntax do i use to set this up? where/how does the .then work in this use-case?

EDIT (added data structure): Instead of firebase push keys i am using numbers so i can randomly select them.

最满意答案

那里你可以使用如下的交易承诺,

var oldPosScore = firebase.database().ref('/data/n:'+stateNum[plus]); oldPosScore.child('score').transaction (function(increment1) { return ((increment1 || 0)*1 + 1); }).then(function(){ alert("transaction successfull"); }).catch(function(err){ alert(err); });

请参阅我的Js小提琴交易示例“然后”承诺

我编辑了我的Js小提琴。 看看它是否适用于你?

there you can use transaction promise like below,

var oldPosScore = firebase.database().ref('/data/n:'+stateNum[plus]); oldPosScore.child('score').transaction (function(increment1) { return ((increment1 || 0)*1 + 1); }).then(function(){ alert("transaction successfull"); }).catch(function(err){ alert(err); });

see my Js Fiddle Sample for Transaction "then" Promise

I have edited my Js fiddle. See this whether it works for you or not?

更多推荐

本文发布于:2023-04-29 23:24:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1336301.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:组件   事务   如何在   firebase   transaction

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!