1

I am using the Mobile First Platform Studio plugin 7.1 with Eclipse and following JSONStore Tutorial.

I understand from this posting that the function calls are asynchronous. It seems that functions such as add, delete, find etc can only be done in the then() immediately following the initialize. In this example, it works fine in that sense but as soon as I do something out of the then() following the initialize, I get an error "PERSISTENT_STORE_NOT_OPEN"

function wlCommonInit() {
    WL.JSONStore.destroy();
    var collectionName = 'people';
    var collections = {};
    collections[collectionName] = {};
    collections[collectionName].searchFields = {
        name: 'string',
        age: 'integer'
    };
    var options = {};
    WL.JSONStore.init(collections, options)
        .then(function() {
        WL.Logger.debug("Collections " + collectionName + " initialized");
        var data = [
            {name: 'Trevor',age: 30},
            {name: 'Allison',age: 28},
            {name: 'Peyton',age: 28}
        ];
        var dataOptions = {};

        WL.JSONStore.get(collectionName).add(data, dataOptions)
            .then(function() {
            WL.Logger.debug("Data added to the collections");
            var query = {
                name: 'Peyton'
            };
            var options = {};
            WL.JSONStore.get(collectionName).find(query, options)
                .then(function(result) {
                WL.Logger.debug("Found: " + result[0]._id + " " + result[0].json.name);
            }).fail(function(result) {
                WL.Logger.error("FIND FAILED: " + result);
            });
        }).fail(function() {
            WL.Logger.error("Data failed to add to the collections");
        });
    }).fail(function() {
        WL.Logger.error("Collections " + collectionName + " failed to initialize");
    });

    // added outside the then()
    var query = {_id: 2};
    var options = {exact: true, limit: 1};
    WL.JSONStore.get(collectionName).find(query, options)
    .then(function(result) {
        WL.Logger.debug("Found: " + result[0]._id + " " + result[0].json.name);
    }).fail(function() {
        WL.Logger.error("FIND BY ID FAILED");
    });
}

My question is how to I cater to a button click and then add data to the JSONStore? That is how can I get access to the store again outside the first then()?

In the code I have a comment //Added outside the then() where the error in question is thrown.

Community
  • 1
  • 1
Spindoctor
  • 434
  • 3
  • 17
  • @Quentin This is not actually a duplicate. – Tomalak Mar 19 '16 at 18:33
  • @Tomalak — Yes, it is. Asynchronous function? check. Code outside the callback trying to access data assigned by the asynchronous function? check. Same problem. Very slightly different expression of it, but still the same problem. – Quentin Mar 19 '16 at 18:35
  • No, not really. The error message says `PERSISTENT_STORE_NOT_OPEN`, which is specific to JSONStore, and nowhere does the OP try to access a value defined inside a callback. – Tomalak Mar 19 '16 at 18:38
  • @Tomalak — While abstracted via a layer or two of library code, that's still trying to access data defined by `init()` which is asynchronous, and it is trying to do it outside of the callback. In the duplicate, the value of `response` isn't defined by the callback either. – Quentin Mar 19 '16 at 18:41
  • @Quentin The callback to `.init()` has no parameters, and inside it the OP calls `WL.JSONStore.get(collectionName)...` just exactly as he tries to do outside of the callback. He does neither try to return a value from that callback, nor use an out-of-scope variable. The difference is that in the callback to `.init()` the store is implicitly open, and outside of the callback it is not. This is an entirely different problem, hence this question is not a duplicate. – Tomalak Mar 19 '16 at 18:47
  • I don't think we're going to agree on where the line is drawn here. – Quentin Mar 19 '16 at 18:48
  • 1
    @Spindoctor Your error comes from the fact that you can't execute `.get()` (etc) without having called `.init()` first (see [docs](https://www.ibm.com/support/knowledgecenter/SSZH4A_6.0.0/com.ibm.worklight.help.doc/devref/c_jsonstore_get.html)). So yes, you need to be in a `.then()` callback of `.init()`, but it does not necessarily have to be the same one. I've linked to a question that should address concerns about calling `.init()` multiple times. – Tomalak Mar 19 '16 at 19:18
  • Thank you @Tomalak. I think this address it. Also I agree that my question is a duplicate of the init() being called multiple times. – Spindoctor Mar 20 '16 at 13:04
  • 1
    @Spindoctor You still might have a timing issue (race condition) when you call init multiple times one after another. I'm not sure how JSONStore reacts to that. You should study the documentation thoroughly and do some experiments to gain confidence which callback runs when. – Tomalak Mar 20 '16 at 13:58
  • Thank you @Tomalak, I'm in the process of experimenting and will watch out for race conditions. – Spindoctor Mar 20 '16 at 20:39

0 Answers0