I am working on node-postgres and there is a strange transactions.
The function aims at locking user's wallet until the refund process is complete, then will update item's has_refund to true. The problem I am facing is the program return no error, but the database is not updated. Unless I spam the function for multiple times then the value is updated. If I remove begin and commit, then the function work perfectly.
constrefundService = (itemId) =>{
awaitpgPool.query('BEGIN;');
constusers = (awaitpgPool.query('SELECT * from app_user where $1=ANY(purchase_list);', [ itemId ])).rows;
for(leti = 0; i < users.length; i++){
refund(users[i])
}
awaitpgPool.query('UPDATE item_lists SET has_refund = $1 where id = $2;', [true, itemId ]);
awaitpgPool.query('COMMIT;');
}
constrefund = (user) =>{
constrefund = 10
awaitpgPool.query('UPDATE app_user SET wallet = wallet + $1', [refund ]);