Skip to content

Basic Object Retrieval

Backendless supports multiple data search and retrieval operations. These include finding an object by its objectId, finding first or last object in the collection or retrieving the entire persisted collection. Each method is available in both blocking and non-blocking formats:

Retrieving Data Objects

Retrieve data objects with the default paging setting from a table:
Backendless.Data.of( "TABLE-NAME" ).find()
 .then( function( result ) {
  })
 .catch( function( error ) {
  });
Find first data object from a table. The first data object is the first one saved in the data store:
Backendless.Data.of( "TABLE-NAME" ).findFirst()
 .then( function( result ) {
  })
 .catch( function( error ) {
  });
Find last data object from a table. The last data object is the last one saved in the data store:
Backendless.Data.of( "TABLE-NAME" ).findLast()
 .then( function( result ) {
  })
 .catch( function( error ) {
  });
Find a data object by its objectId:
Backendless.Data.of( "TABLE-NAME" ).findById( objectId )
 .then( function( result ) {
  })
 .catch( function( error ) {
  });
Define a function/class which will represent a data table on the server:
function X() {
  // properties of the class defined here
}
Retrieve data objects with the default paging setting from a table. Returned collection will contain objects of type X (the name of the class must match the name of the table):
Backendless.Persistence.of( X ).find()
  .then( function( result ) {
   })
  .catch( function( error ) {
   });
Find first data object from a table. The name of the class X must match the name of the table. The first data object is the first one saved in the data store:
Backendless.Persistence.of( X ).findFirst()
  .then( function( result ) {
   })
  .catch( function( error ) {
   });
Find last data object from a table. The name of the class X must match the name of the table. The last data object is the last one saved in the data store:
Backendless.Persistence.of( X ).findLast()
  .then( function( result ) {
   })
  .catch( function( error ) {
   });
Find a data object by its ID. The name of the class X must match the name of the table:
Backendless.Persistence.of( X ).findById( objectId )
  .then( function( result ) {
   })
  .catch( function( error ) {
   });

where:

Argument                Description
X a reference to a JavaScript constructor function defining a JavaScript class. Object(s) returned by functions will be of the specified type.
TABLE-NAME name of the table where to retrieve object(s) from.

Return Value

The find method returns an array of objects. All other methods return a single object.

Example

The following code demonstrates various search queries:

Load contacts using default paging

Backendless.Data.of( "Contact" ).find()
  .then( function( result ) {
     // every loaded object from the "Contact" table is now an individual untyped
     // JS object in the "result" array
   })
  .catch( function( error ) {
    // an error has occurred, the error code can be retrieved with fault.statusCode
   });

Find first contact

Backendless.Data.of( "Contact" ).findFirst()
 .then( function( firstObject ) {
    // first contact instance has been found
  })
 .catch( function( error ) {
    // an error has occurred, the error code can be retrieved with fault.statusCode
  });

Find last contact

Backendless.Data.of( "Contact" ).findLast()
 .then( function( lastObject ) {
    // last contact instance has been found
  })
 .catch( function( error ) {
    // an error has occurred, the error code can be retrieved with fault.statusCode
  });

Find contact by objectId

Backendless.Data.of( "Contact" ).findById( {objectId:"XXXX-XXXX-XXXX-XXXX" } )
 .then( function( contactObject ) {
    // contact instance has been found by its objectId
  })
 .catch( function( error ) {
    // an error has occurred, the error code can be retrieved with fault.statusCode
  });
Define the constructor function first:
function Contact()
{
  this.objectId = "";
  this.name = "";
  this.age = 0;
  this.phone = "";
  this.title = "";
}
The following code demonstrates various search queries:

Load contacts using default paging

Backendless.Data.of( Contact ).find()
  .then( function( result ) {
     // every loaded object from the "Contact" table is now an individual 
     // instance of Contact in the "result" array
   })
  .catch( function( error ) {
    // an error has occurred, the error code can be retrieved with fault.statusCode
   });

Find first contact

Backendless.Data.of( Contact ).findFirst()
 .then( function( firstContact ) {
    // first contact instance has been found
  })
 .catch( function( error ) {
    // an error has occurred, the error code can be retrieved with fault.statusCode
  });

Find last contact

Backendless.Data.of( Contact ).findLast()
 .then( function( lastObject ) {
    // last Contact instance has been found
  })
 .catch( function( error ) {
    // an error has occurred, the error code can be retrieved with fault.statusCode
  });

Find contact by objectId

Backendless.Data.of( Contact ).findById( {objectId:"XXXX-XXXX-XXXX-XXXX" } )
 .then( function( contactObject ) {
    // Contact instance has been found by its objectId
  })
 .catch( function( error ) {
    // an error has occurred, the error code can be retrieved with fault.statusCode
  });