Topic
  • 4 replies
  • Latest Post - ‏2013-01-04T16:06:22Z by WJK1_Barani_Ramakrishnan
WJK1_Barani_Ramakrishnan
8 Posts

Pinned topic WL.JSONStore is not working

‏2013-01-03T18:47:40Z |
Hi,

I have been trying to implement Collection storage in WL 5.0.5. I followed IBM instructions.
I made one sample hybrid applications and tried to init collections then getting following exception.

Error validating schema
java.lang.Throwable: node with name "_id" already exists in schema

I am using android emulator to test above scenario. If anyone has experienced problem, Please let me know.

Thanks in advance.

Note: I have created new proj and new hybrid applications for testing. Still error remain same. Only IBM sample applications work fine.
  • SystemAdmin
    SystemAdmin
    2327 Posts

    Re: WL.JSONStore is not working

    ‏2013-01-03T19:06:54Z  
    Can you attach your project or copy the code here?
  • WJK1_Barani_Ramakrishnan
    8 Posts

    Re: WL.JSONStore is not working

    ‏2013-01-03T20:38:37Z  
    Can you attach your project or copy the code here?
    Thanks for quick response. Please use below code.

    window.$ = window.jQuery = WLJQ;

    function wlCommonInit(){

    /*
    * Application is started in offline mode as defined by a connectOnStartup property in initOptions.js file.
    * In order to begin communicating with Worklight Server you need to either:
    *
    * 1. Change connectOnStartup property in initOptions.js to true.
    * This will make Worklight framework automatically attempt to connect to Worklight Server as a part of application start-up.
    * Keep in mind - this may increase application start-up time.
    *
    * 2. Use WL.Client.connect() API once connectivity to a Worklight Server is required.
    * This API needs to be called only once, before any other WL.Client methods that communicate with the Worklight Server.
    * Don't forget to specify and implement onSuccess and onFailure callback functions for WL.Client.connect(), e.g:
    *
    * WL.Client.connect({
    * onSuccess: onConnectSuccess,
    * onFailure: onConnectFailure
    * });
    *
    */
    // Common initialization code goes here
    alert('another test');
    WL.Logger.debug("Starting...");
    if ((WL.Client.getEnvironment() === 'iphone' ||
    WL.Client.getEnvironment() === 'ipad' ||
    WL.Client.getEnvironment() === 'android') &&
    typeof cordova !== 'undefined' &&
    typeof WL.JSONStore !== 'undefined' ) {

    var col = initializeCollection();
    alert(col);
    addToCollection(col);
    read(col);
    }

    }

    function initializeCollection(){

    var collectionName = 'AnotherTest',
    searchFields = {name: 'string', age: 'number'};

    var failureCall = function(){
    alert("function failure");
    };

    var win = function (data) {
    alert('Collection init successful: ' + data);
    };

    var options = {onSuccess: win, onFailure: failureCall};

    var itemListCollection = WL.JSONStore.initCollection(collectionName, searchFields, options);
    return itemListCollection;

    }
    function addToCollection(myCol){

    var failureCall = function(){
    alert("function failure");
    };

    var win = function (data) {
    alert('Collection add successful: ' + data);
    };
    var options = {onSuccess: win, onFailure: failureCall};

    myCol.add({name: "123456", age: "xypzasdlkjaf#="},options);
    }

    function read(myCol){

    var failureCall = function(){
    alert("function failure");
    };

    var win = function (data) {
    alert('Collection findAll successful: ' + data);
    };
    var options = {onSuccess: win, onFailure: failureCall};

    myCol.findAll(options);
    }
  • SystemAdmin
    SystemAdmin
    2327 Posts

    Re: WL.JSONStore is not working

    ‏2013-01-03T21:48:47Z  
    Thanks for quick response. Please use below code.

    window.$ = window.jQuery = WLJQ;

    function wlCommonInit(){

    /*
    * Application is started in offline mode as defined by a connectOnStartup property in initOptions.js file.
    * In order to begin communicating with Worklight Server you need to either:
    *
    * 1. Change connectOnStartup property in initOptions.js to true.
    * This will make Worklight framework automatically attempt to connect to Worklight Server as a part of application start-up.
    * Keep in mind - this may increase application start-up time.
    *
    * 2. Use WL.Client.connect() API once connectivity to a Worklight Server is required.
    * This API needs to be called only once, before any other WL.Client methods that communicate with the Worklight Server.
    * Don't forget to specify and implement onSuccess and onFailure callback functions for WL.Client.connect(), e.g:
    *
    * WL.Client.connect({
    * onSuccess: onConnectSuccess,
    * onFailure: onConnectFailure
    * });
    *
    */
    // Common initialization code goes here
    alert('another test');
    WL.Logger.debug("Starting...");
    if ((WL.Client.getEnvironment() === 'iphone' ||
    WL.Client.getEnvironment() === 'ipad' ||
    WL.Client.getEnvironment() === 'android') &&
    typeof cordova !== 'undefined' &&
    typeof WL.JSONStore !== 'undefined' ) {

    var col = initializeCollection();
    alert(col);
    addToCollection(col);
    read(col);
    }

    }

    function initializeCollection(){

    var collectionName = 'AnotherTest',
    searchFields = {name: 'string', age: 'number'};

    var failureCall = function(){
    alert("function failure");
    };

    var win = function (data) {
    alert('Collection init successful: ' + data);
    };

    var options = {onSuccess: win, onFailure: failureCall};

    var itemListCollection = WL.JSONStore.initCollection(collectionName, searchFields, options);
    return itemListCollection;

    }
    function addToCollection(myCol){

    var failureCall = function(){
    alert("function failure");
    };

    var win = function (data) {
    alert('Collection add successful: ' + data);
    };
    var options = {onSuccess: win, onFailure: failureCall};

    myCol.add({name: "123456", age: "xypzasdlkjaf#="},options);
    }

    function read(myCol){

    var failureCall = function(){
    alert("function failure");
    };

    var win = function (data) {
    alert('Collection findAll successful: ' + data);
    };
    var options = {onSuccess: win, onFailure: failureCall};

    myCol.findAll(options);
    }
    I highly recommend you read the API docs:
    http://pic.dhe.ibm.com/infocenter/wrklight/v5r0m5/topic/com.ibm.worklight.help.doc/apiref/r_class_wl_jsonstore.html

    It clearly states in the API doc for initCollection that:
    "The collection will not be usable until a successful callback."
    http://pic.dhe.ibm.com/infocenter/wrklight/v5r0m5/index.jsp?topic=%2Fcom.ibm.worklight.help.doc%2Fapiref%2Fr_wl_jsonstore_initcollection.html

    I re-wrote your code to work with callbacks without changing your 'addToCollection' and 'read' functions. You should also note that the failure callbacks return an error code that can be passed to WL.JSONStore.getErrorMessage, that can help you figure out what's wrong and you will be able to provide more information if you post on the forums. As a side note, the data types you pass to as search fields to initCollection (searchFields = {name: 'string', age: 'number'}) are just suggestions that may affect how we store data, if you need validation you should implement yourself. I mention this because you're passing a String value to age, but said you where going to pass Numbers there.

    Here's the re-written code, tested on IOS6 and Android 2.2:

    
    window.$ = window.jQuery = WLJQ;   function wlCommonInit () 
    {   WL.Logger.debug(
    "Starting...");   
    
    if ((WL.Client.getEnvironment() === 
    'iphone' || WL.Client.getEnvironment() === 
    'ipad' || WL.Client.getEnvironment() === 
    'android') && typeof cordova !== 
    'undefined' && typeof WL.JSONStore !== 
    'undefined'        ) 
    {   var col = initializeCollection(function (collection) 
    { 
    //the collection can only be used AFTER the onSuccess callback 
    
    if (collection === 
    
    null) 
    { 
    //log an error if the collection was not initialized WL.Logger.debug(
    "Error starting the collection"); 
    } 
    
    else 
    { 
    //after the collection exists we can try to add data addToCollection(col, function (data) 
    { 
    
    if(data === 
    
    null) 
    { 
    // log errors if you want WL.Logger.debug(
    "Error adding to the collection"); 
    } 
    
    else 
    { 
    //do something with data (number of elements added) 
    } read(col); 
    //read using the collection variable 
    });      
    //end addToCollection 
    } 
    }); 
    //end initializeCollection 
    } 
    //end if statement 
    }
    //end wlCommonInit   function initializeCollection (callback) 
    {   var itemListCollection = null, collectionName = 
    'AnotherTest', searchFields = 
    {name: 
    'string', age: 
    'number'
    };   var failureCall = function (err) 
    { alert(
    "initializeCollection failure" + err + WL.JSONStore.getErrorMessage(err)); callback(
    
    null); 
    };   var win = function (data) 
    { alert(
    'Collection init successful: ' + data); callback(itemListCollection); 
    };   var options = 
    {onSuccess: win, onFailure: failureCall
    };   itemListCollection = WL.JSONStore.initCollection(collectionName, searchFields, options); 
    
    return itemListCollection; 
    }   function addToCollection (myCol, callback) 
    {   var failureCall = function (err) 
    { alert(
    "addToCollection failure" + err + WL.JSONStore.getErrorMessage(err)); callback(
    
    null); 
    };   var win = function (data) 
    { alert(
    'Collection add successful: ' + data); callback(data); 
    }; var options = 
    {onSuccess: win, onFailure: failureCall
    };   myCol.add(
    {name: 
    "123456", age: 
    "xypzasdlkjaf#="
    },options); 
    }   function read (myCol) 
    {   var failureCall = function (err) 
    { alert(
    "read failure" + err + WL.JSONStore.getErrorMessage(err)); 
    };   var win = function (data) 
    { alert(
    'Collection findAll successful: ' + JSON.stringify(data)); 
    }; var options = 
    {onSuccess: win, onFailure: failureCall
    };   myCol.findAll(options); 
    }
    
  • WJK1_Barani_Ramakrishnan
    8 Posts

    Re: WL.JSONStore is not working

    ‏2013-01-04T16:06:22Z  
    It works fine after I used callback methods