Async data save

Hello everyone, back again with another question. So, I have a trigger that calls a custom event, meant to be asynchronous. That event generates a new instance of an object Order that I have created and tries to save it in the database.
Now, my event and triggers have the following code:
http://paste.ofcode.org/U5b8LCT65G5HfnSik9ZFtP
The problem is that the object gets saved, I get a duplicate error if it’s in the database already, but the success function never gets entered.
Could you please point me in the right direction?
Kind regards,
Dragos

Just to point out that it works with the example in the docs, where the user is saved asynchronously

The success function is gonna be called only in case of successful database insertion.

In case of an error the error function is called

Hi Vitaly.

I know, and that’s what I expect to happen. However, the Order object gets inserted into the database but the success function never gets called. In case of a duplicate, the error function is also not called, but the error does get printed out.

Could I get an example of how to save a custom object to the database, asynchronously, from within a custom event that gets called from a trigger?

Could you please show me the whole CodeRunner session output ?

Btw, I assume you are working in debug mode for now, right ?

Yes, I am working in debug mode. Apologies, the old code that I have tried did not enter the error function, but the snippet I posted does call upon it when an error occurs. However, still not entering the success function on the initial save call.

The output is something like this:

Lines 21-22 represent the moment when the order object gets stored into the database, and the next task that gets processed enters the error function as the object is already stored in the database.


Debugger listening on port 58941
16:25:27.887 - CodeRunner(tm) Backendless Debugging Utility v1.7.4
16:25:27.889 - Copyright(C) 2016 Backendless Corp. All rights reserved. 
16:25:28.146 - Starting Debug Code Runner...
16:25:28.165 - Building Model..
16:25:28.593 - Model Build completed
16:25:28.595 - Event handlers (2):
16:25:28.595 -   custom.kctuevent (async) (app/handlers/custom-events/kctuevent.js)
16:25:28.595 -   persistence.beforeCreate (Order) (app/handlers/persistence/order/beforeCreate.js)
16:25:28.595 - Timers (1):
16:25:28.595 -   Notification (app/timers/notification.js)
16:25:28.595 - Custom Types (1):
16:25:28.596 -   Order (app/models/order.js)
16:25:28.771 - Registering Code Runner on https://api.backendless.com
16:25:32.866 - Runner successfully registered.
16:25:32.866 - Registering Model on https://api.backendless.com
16:25:33.256 - Model successfully registered
16:25:33.256 - Waiting for Server Code tasks..
16:25:54.453 - [63726F22-F8B5-EBFB-FFC9-18274DA8F900] New task arrived!
16:25:54.465 - [63726F22-F8B5-EBFB-FFC9-18274DA8F900] Processing finished
16:26:00.374 - [9EACB00C-4249-748B-FF88-14580DA9B900] New task arrived!
16:26:00.377 - [9EACB00C-4249-748B-FF88-14580DA9B900] Processing finished
error message - Duplicate entry
error code - undefined

Please, enable promises, and change your custom event handler to return a Promise

//at the top of the file
Backendless.enablePromises();
//in custom event handler
var testOrder = new Order({name: req.name, owner: req.context});
return Backendless.Persistence.of(Order).save(testOrder).then(orderRegistered, gotError);

Timer also should return a promise to tell the CodeRunner about asynchronous code execution

//in timer 
 execute(req){
 return customEventHandler({name: "Name", email: "email@address.com"}); 
 }

http://paste.ofcode.org/3693L2DW9X5KcedCEKTmbpW

You can read more about asynchronous Business Logic execution flow here