Page 1 of 1

add 1 to existing entry in database collection

Posted: Wed Aug 19, 2015 1:45 pm
by Joe Sharples

how can I +1 to the existing number in the collection?

when a user clicks a button I want to use an update service to +1 the existing number in a certain field for a certain row.

at the moment I have a read service on page show that maps the existing number to a label, and on update service mapping I have the code
pre
var numbB4 = value;

var newNum = numbB4 + 1;

return newNum;
/pre

This method has a flaw though. If a user1 opens the page, invokes the read service, then sits idle for 30 mins, then invokes the update service. they will be using the existing value from the label that is 30 mins old. in this time user2 could have opened the page, invoked the read service, and invoked the update service.
I could have an additional read service called just before the update service on button click, to retrieve the latest 'existing number'. but I wan't to reduce the amount of api calls.

so is there a way that the update service can just +1 to what ever value is there, rather than removing it and replacing it with a new one?

Thanks


add 1 to existing entry in database collection

Posted: Wed Aug 19, 2015 7:18 pm
by Serhii Kulibaba

Hello Joe,

Please use $inc operation: https://devcenter.appery.io/documenta...


add 1 to existing entry in database collection

Posted: Fri Aug 21, 2015 10:23 pm
by Joe Sharples

Hi Sergiy,

Thank you,
I looked at the documentation but couldn't get it to work.
Image

This didn't work the 'NumberOfCallsMade' was still blank. I tired again but put a number in the field before testing, but the number was till the same after testing.
I tried it in the app too and there was no error.

not sure what i'm doing wrong.


add 1 to existing entry in database collection

Posted: Mon Aug 24, 2015 12:42 pm
by Serhii Kulibaba

What is the NumberOfCallsMade's type? Is it Number? It can't be empty fo $inc. You have to set it as default, e.g. = 0


add 1 to existing entry in database collection

Posted: Mon Aug 24, 2015 11:05 pm
by Joe Sharples

yes NumberOfCallsMade's type is number.
as i mentioned before I tried having a default number before testing and it still did not work


add 1 to existing entry in database collection

Posted: Tue Aug 25, 2015 1:30 am
by M&M

well, you can actually do a fetch before updating the value. That way the value will be the most current value. As an alternative you can use a simple, small piece of servercode. Because it runs on the server - everytime it is called, simply do a lookup, or fetch the value from the database and increment it by one.

Here is some documentation on server code: https://devcenter.appery.io/tutorials...

I think the examples there are of GET. You will perhaps be using POST as GET technically (or as per the rules) isn't supposed to be modifying data.