Search code examples
ibm-mobilefirstworklight-adaptersjsonstoreworklight-security

How to get or return the values from database in Json format in jsonstore Worklight 6.2?


I have written the Json store for Getting adding replacing deleting values in Database. i have initialized the collection.like this

function wlCommonInit(){
    var collections = {
       Users : {
           searchFields: { Username: 'integer', Password: 'string'}
       },
       adapter:{
           name:'SQL',
           add:'addSQL',
           remove:'deleteSQL',
           replace:'updateSQL',
           load:{
               procedure:'getSQLs',
               params:[],
           }
       }
   };
   WL.JSONStore.init(collections).then(function () {
      alert("Collection is declared");
   });}

   $("#get").bind("click",function(){
       WL.JSONStore.get("Users").load().then(function(res1){
           alert(JSON.stringify(res1));
       });
   });

Adapter:

var selectStatement = WL.Server.createSQLStatement("select username, password from test");

function getSQLs() {

return WL.Server.invokeSQLStatement({
    preparedStatement : selectStatement,
    parameters : []
});}

While Running this i m getting error like:

[wl.jsonstore] {"src":"load","err":18,"msg":"FAILED_TO_LOAD_INITIAL_DATA_FROM_ADAPTER_INVALID_LOAD_OBJ","col":"Users","usr":"jsonstore","doc":{},"res":{}}

Can you please let me know how do i return the value from database and how do i resolve this error.

Regards,

Sheikh Mohammed Shamnoon


Solution

  • You're missing the load key which makes your load object invalid, just like the error message says (FAILED_TO_LOAD_INITIAL_DATA_FROM_ADAPTER_INVALID_LOAD_OBJ).

    You have:

    load:{ procedure:'getSQLs', params:[], }

    You should have:

    load:{ procedure:'getSQLs', params:[], key: yourLoadKey }

    It depends on what your getSQLs procedure calls returns, this how the key fits into the adapter response: responseFromAdapter.invocationResult[key].

    However, since your using v6.2 I would suggest you avoid using the load and push JSONStore APIs and follow the documentation here for working with external data.

    Some sample code based on the documentation link above:

    WL.JSONStore.init(collections)
    
    .then(function () {
    
        alert('init done');
    
        return WL.Client.invokeProcedure({
          adapter : 'SQL',
          procedure : 'getSQLs',
          parameters : []
        });
    })
    
    .then(function (responseFromAdapter) {
    
        alert('responseFromAdapter:' + JSON.stringify(responseFromAdapter));
    
        //Look at the responseFromAdapter and call something like
        //WL.JSONStore.get('Users').add(...)
    });