How to properly initialize the JSON store in Workl

2019-09-01 00:23发布

问题:

I am attempting to initalize the IBM Worklight JSON store as below:

//JSONStore jsonStoreCollection metadata
var jsonStoreCollection = {};

//JSONStore jsonStoreCollection metadata
var COLLECTION_NAME = 'people';

function wlCommonInit(){


    // Create empty options to pass to
    // the WL.JSONStore.init function
    var options = {};

    //Define the collection and list the search fields
    jsonStoreCollection[COLLECTION_NAME] = {
        searchFields : {name: 'string'},
    };


    //Initialize the JSON store collection
    WL.JSONStore.init(jsonStoreCollection, options)
    .then(function () {
        console.log("Successfully Initialized the JSON store");
    })
    .fail(function (errorObject) {
        console.log("JSON store init failed :( ");
    });

}

But when I run this in my android emulator the logcat gives me the "JSON store init failed" message. And the following error:

[wl.jsonstore {"src":"initCollection", "err":-2,"msg":"PROVISION_TABLE_SEARCH_FIELDS_MISMATCH","col":"token","usr":"jsonstore","doc":{},"res":{}}

This implementation seems to be very much what is outlined in the documentation, however I cannot get it to initialize.

Can anyone tell me what I am doing wrong here?

回答1:

The documentation with the error codes is here.

-2 PROVISION_TABLE_SEARCH_FIELDS_MISMATCH

Search fields are not dynamic. It is not possible to change search fields without calling the destroy method or the removeCollection method in the WL.JSONStore class before calling the init method with the new search fields. This error can occur if you change the name or type of the search field. For example: {key: 'string'} to {key: 'number'} or {myKey: 'string'} to {theKey: 'string'}.

No need to uninstall the application, just follow the documentation and handle that error case by calling removeCollection or destroy. For example:

WL.JSONStore.init(...)
.then(function () {
  //init was successful
})
.fail(function (error) {
  //check for -2
  //call removeCollection or destroy
  //re-init with new search fields
});

You can always submit a feature request to make this easier.



回答2:

If you have previously created a JSON store with the same name but with different initialization variables. You must uninstall the application.

After uninstalling you can re-deploy the application to the device and the JSON store will initialize as expected.

Since discovering this, I have seen the issue a couple more times as I made changes to the configuration of my JSON store in my Worklight application.