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 ) {
});
Backendless.Data.of( "TABLE-NAME" ).findFirst()
.then( function( result ) {
})
.catch( function( error ) {
});
Backendless.Data.of( "TABLE-NAME" ).findLast()
.then( function( result ) {
})
.catch( function( error ) {
});
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
}
Backendless.Persistence.of( X ).find()
.then( function( result ) {
})
.catch( function( error ) {
});
Backendless.Persistence.of( X ).findFirst()
.then( function( result ) {
})
.catch( function( error ) {
});
Backendless.Persistence.of( X ).findLast()
.then( function( result ) {
})
.catch( function( error ) {
});
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¶
``` js
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¶
``` js
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¶
``` js
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¶
``` js
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 = "";
}
Load contacts using default paging¶
``` js
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¶
``` js
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¶
``` js
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¶
``` js
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
});
```