Skip to content

Conditional Delivery of New Objects

Consider the following data table. The table name is Order and it contains the orderAmount column which will be used in the example:

order-table-rt.zoom70

The code below adds a listener for the create event for the Order table.

const orderEventHandler = Backendless.Data.of( 'Order' ).rt();

const onObjectCreate = object => 
                       console.log( 'New Order object has been created. Object ID - ', object.objectId );

const onError = error => 
                console.log( 'Server reported an error', error );

orderEventHandler.addCreateListener( 'orderAmount > 1000', onObjectCreate, onError );
function Order() {
  this.objectId = undefined;
  this.orderAmount = 0;
}

const orderEventHandler = Backendless.Data.of( Order ).rt();

const onObjectCreate = object => 
                       console.log( 'New Order object has been created. Object ID - ', object.objectId );

const onError = error => 
                console.log( 'Server reported an error', error );

orderEventHandler.addCreateListener( 'orderAmount > 1000', onObjectCreate, onError );

The event listener will receive only the Order objects where the amount of order is greater than 1000. This is achieved with the first argument in the addCreateListener method. The argument is a where clause condition, which references the orderAmount column and requests that the real-time database sends only the matching objects. The second argument is a callback object which will receive any new object which matches the where clause condition. Notice the argument of the response callback method is the actual object saved in the database.

For more information about the whereClause syntax, see the Condition Syntax section of the guide.

Codeless Reference

rt_data_overview_1

where:

Argument                Description
id The unique identifier of the event listener.
table name The name of the data table where the event listener must be registered.
where clause Optional parameter. Determines under what conditions the listener is triggered. When this property is not specified, then the listener will retrieve any new object created in the database. For more information about the where clause condition refer to the Search With The Where Clause topic.
object When a listener gets triggered, the Backendless delivers the newly created object to the listener.

The example below registers a new event listener in the "Order" data table. The listener is triggered when the where clause condition is met, specifically when a new object is created in the data table and the value in the "orderAmount" property is greater than 1000. Note that when a new object is created in the data table and the listener is triggered, the Backendless delivers this object to the listener.

Assume the where clause condition is met, and the Codeless logic below executes a custom block of code; First the following sentence is printed: "New Order object has been created. ObjectId:", then the value of the property objectId is retrieved from the object variable and also gets printed.

rt_data_conventional_delivery