我使用的是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?
更多推荐
发布评论