Search code examples
ibm-mobilefirstjsonstore

Worklight JsonStore advanced find


How to use advanced find in worklight JSONStore using QueryPart?

I have tried the following code but its not working properly, I doubt if I am calling advancedFind correctly.

var query = WL.JSONStore.QueryPart().equal('age', 35);
var collectionName = "people";

WL.JSONStore.get(collectionName).find(query).then(function(arrayResults) {
    // if data not present , get the data from DB
    if (arrayResults.length == 0) {
        } else {

            }
}).fail(function(errorObject) {
    alert("fail" + errorObject);
    // handle failure
});

Solution

  • You are calling the find() method. The one you want to call is advancedFind(). Also, advancedFind receives an array of query parts, not just one query part. Your code should look like this:

    var queryPart = WL.JSONStore.QueryPart().equal('age', 35);
    var collectionName = "people";
    
    WL.JSONStore.get(collectionName).advancedFind([queryPart]).then(function(arrayResults) {
         // if data not present , get the data from DB
         if (arrayResults.length == 0) {
    
         } else {
    
         }
    }).fail(function(errorObject) {
         alert("fail" + errorObject);
         // handle failure
    });
    

    For future reference, here is the API and some examples on how to use the Javascript JSONStore API.