In my firebase cloud functions code :
return num_picks_ref.transaction(function(cur_num_picks) {
console.log(`cur_num_picks: ${cur_num_picks}`)
console.log(`cur_num_picks <= 0: ${cur_num_picks <= 0}`)
if (cur_num_picks <= 0) {
console.error(`user ${uid} is submitting picks too fast, reached negative pick balance.`)
}
return cur_num_picks - 1
})
Then in the output logs, I get :
12:46:35.519 PM ---- db_positions_decrementPickCount ---- cur_num_picks: 87
12:46:35.420 PM ---- db_positions_decrementPickCount ---- user KtY8U5e1p8MCJDOFW2eEuLW5DO83 is submitting picks too fast, reached negative pick balance.
12:46:35.416 PM ---- db_positions_decrementPickCount ---- cur_num_picks: null
I understand that transactions can be called multiple times to guarantee atomic writes, but this is the only code modifying cur_num_picks
in the environment. Nowhere else is modifying that value in the realtime DB.
It seems the functions are not run in the order specified which does not make sense to me.
Most importantly, cur_num_picks
evaluates to 87 in the first line, but not 87 again in the next line. How can this be?