Skip to content

Conditional Delivery of Updated Objects

Consider a data table called Order. The table contains a column named orderAmount. The code below adds a listener for the update event for the Order table.

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

const onObjectUpdate = object => 
                       console.log( 'an Order object has been updated. Object ID - ', object.objectId );

const onError = error => 
                console.log( 'An error has occurred -', error );

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

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

const onObjectUpdate = object => 
                       console.log( 'an Order object has been updated. Object ID - ', object.objectId );

const onError = error => 
                console.log( 'An error has occurred -', error );

orderEventHandler.addUpdateListener( 'orderAmount > 1000', onObjectUpdate, onError );

The event listener will receive only the Order objects which are being updated in the database where the amount of order is greater than 1000. This is achieved with the first argument in the addUpdateListener 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 updated object which matches the where clause condition. Notice the argument of the response callback method is the actual object updated in the database.

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