Azure insert script not working -
i have 2 tables in azure database , trying add script user can insert data 1 table field every time entered table transactions. tables in different schemas in same database.
when row entered in database transactions, need check if user account row in table 'usertable', fetch record usertable , replace data in field 'balance'
i have tried following script nothing happens. ideas doing wrong?
function insert(item, user, request) { var usertable = tables.gettable('usertable'); var total = item.amount + usertable.balance; var data = { balance: total, }; if(usertable.useraccount === item.useraccount) { usertable.insert(data); } request.execute(); }
the usertable
object tables.gettable
gives reference table object. doesn't have balance
or useraccount
property you're trying access. need first query usertable, result make updates. script below should similar need.
function insert(item, user, request) { var usertable = tables.gettable('usertable'); usertable.where({ useraccount: item.useraccount }).read({ success: function(results) { if (results.length === 0) { // new user. can either return error, or add // new entry. add new entry here. usertable.insert({ balance: item.amount }, { success: function() { // insert transaction request.execute(); } }); } else if (results.length === 1) { // found it. update result var useraccount = results[0]; var total = useraccount.balance + item.amount; useraccount.balance = total; usertable.update(useraccount, { success: function() { // insert transaction request.execute(); } }); } else { // wrong request.respond( statuscodes.internal_server_error, { error: 'multiple users same account' }); } } }); }
Comments
Post a Comment