Joe Sharples
Posts: 0
Joined: Mon Aug 18, 2014 1:31 pm

add 1 to existing entry in database collection

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

Joe Sharples
Posts: 0
Joined: Mon Aug 18, 2014 1:31 pm

add 1 to existing entry in database collection

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.

Serhii Kulibaba
Posts: 150
Joined: Tue Aug 27, 2013 1:47 pm

add 1 to existing entry in database collection

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

Joe Sharples
Posts: 0
Joined: Mon Aug 18, 2014 1:31 pm

add 1 to existing entry in database collection

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

M&M
Posts: 0
Joined: Tue Nov 11, 2014 6:59 am

add 1 to existing entry in database collection

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.

Return to “Issues”