Question

I am trying to learn JSONStore and in process of that I am trying to execute a piece of code which will first check if a particular JSONStore is already there in the device or not and based on the result it will execute if-else statement. If it is not there, it will create one and add some data in it. In case the jsonStore is already present, code will replace the previously stored data with new data. But when I am trying to execute the code, my device shows the html contents for a while and then the screen gets blank. When I checked the logcat, I didn't get any of my console log statements which I have added in my code. Can anyone please help me in understanding this behavior and what could be done to achieve the requirement.

     var JSONStoreCollections = {};
     var collectionName = 'Person';

    function wlCommonInit(){
require([ "layers/core-web-layer", "layers/mobile-ui-layer" ], dojoInit);

     }

    function dojoInit() {
 require([ "dojo/ready", "dojo/parser", "dojox/mobile", "dojo/dom", "dijit/registry",                "dojox/mobile/ScrollableView" ], function(ready) {
 ready(function() {

    if(!(WL.JSONStore.get(collectionName))){
        console.log("i am in if codition");

                   var Data={
                               Name:'name',
                               Age:27
                          };

         JSONStoreCollections[collectionName] = {};
        JSONStoreCollections[collectionName].searchFields = {Name: 'string'};
        WL.JSONStore.init(JSONStoreCollections)
                                    .then(function () {
                                    console.log("store created");
                                    })
                                    .fail(function (errorObject) {
                                    console.log("store creation failed");
                                    });

                       WL.JSONStore.get(collectionName).add(Data)
                       .then(function () {
                        console.log("data added");
                            })
                            .fail(function (errorObject) {
                            console.log("data addition failed");
                            });
                       var query = {Name: 'name'};

                        WL.JSONStore.get(collectionName)
                        .find(query)
                        .then(function (arrayResults) {

                            console.log(arrayResults);
                            WL.Logger.debug(arrayResults);

                        })
                        .fail(function (errorObject) {
                            console.log(errorObject);

                            WL.Logger.debug(errorObject);
                        });             

                    }
               else{
                       var Data1={
                               Name:'name1',
                               Age:30
                          };

                       WL.JSONStore.get(collectionName).replace(Data1)
                       .then(function () {
                        console.log("data replaced");
                            })
                            .fail(function (errorObject) {
                            console.log("data replacement failed");
                            });

                                           var query = {Name: 'name1'};

                        WL.JSONStore.get(collectionName)
                        .find(query)
                        .then(function (arrayResults) {

                            console.log(arrayResults);
                            WL.Logger.debug(arrayResults);

                        })
                        .fail(function (errorObject) {
                            console.log(errorObject);

                            WL.Logger.debug(errorObject);
                        });                                      


               }

    });
});
}
Was it helpful?

Solution

  1. You need to initialize first, otherwise WL.JSONStore.get(collectionName) will always return undefined. If you never initialize JSONStore, you can not use it.
  2. The replace API works with JSONStore documents (objects that have keys for _id and json).
  3. You only need one .fail, the error object will tell you the source of the error (errorObject.src).

See the untested pseudocode below for what you want to do:

function wlCommonInit () {

var collectionName = 'Person';

var Data = {
 Name: 'name',
 Age: 27
};

var JSONStoreCollections = {};
JSONStoreCollections[collectionName] = {};
JSONStoreCollections[collectionName].searchFields = {Name: 'string'};

WL.JSONStore.init(JSONStoreCollections)

.then(function () {
  WL.Logger.debug('Init done');
  return WL.JSONStore.get(collectionName).findAll();
})

.then(function (res) {
  WL.Logger.debug('Find All returned:', res);

  if (res.length < 1) {

    return WL.JSONStore.get(collectionName).add(Data);

  } else {

    res[0].json = {
      Name:'name1',
      Age:30
    };

    return WL.JSONStore.get(collectionName).replace(res[0]);
  }

})

.then(function () {
  WL.Logger.debug('Add or Replace done');
  return WL.JSONStore.get(collectionName).find({Name: 'name'});
})

.then(function (res) {
  WL.Logger.info('Final Find returned:', res);
})

.fail(function (err) {
  WL.Logger.error(err);
});

}

Expected output the first time executed:

Init done
Find All returned: []
Add or Replace done
Final Find returned: [{"_id":1,"json":{"Name":"name","Age":27}}] 

Expected output other than the first time executed:

Init done
Find All returned: [{"_id":1,"json":{"Name":"name","Age":27}}]
Add or Replace done
Final Find returned: [{"_id":1,"json":{"Name":"name1","Age":30}}] 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top