JS Atomic counters API increment field

Hi,
I want to increment field ‘venue_id’ using atomic counter
this is my code

Backendless.ServerCode.Persistence.beforeCreate('venues_table', function(req) {
//add your code here
var successCallback = function( response )
{
console.log( "[ASYNC] counter value is - " + response );
req.item.venue_id = response;
};
var failureCallback = function( fault )
{
console.log( "error - " + fault.message );
};
var callback = new Backendless.Async( successCallback, failureCallback );
// ************************************************
// Backendless.Counters.of() approach
// ************************************************
var myCounter = Backendless.Counters.of( "venue_counter" );
// async call
myCounter.incrementAndGet( callback );
});

when I am invoking beforeCreate, on successful increment callback I am assigning the value on #line6 of the counter to the field name, however nothing happens. Kindly help

Hi Marlen

CodeRunner invokes your business logic method.
Your business logic starts an asynchronous job ( counter’s incrementAndGet) and returns the control to the CodeRunner.

CodeRunner at this step must decide what it should do next. There are two options :

  • report to the Backendless Server that Business Logic was successfully executed and pass changed item (at this moment it’s not changed yet )
  • wait for asynchronous job complete and perform option one

In your case you need the CodeRunner to perform second option. And to do this, you have to tell the CodeRunner when the asynchronous job is finished.
You have to return a Promise from your method, resolved only after you modify the req.item

It should look like this :

http://take.ms/gNBks</img>
http://pastebin.com/47CGdMb2

See more:
Sync vs Async
CodeRunner Troubleshooting Guide