【问题标题】:JSONStore code not workingJSONStore 代码不起作用
【发布时间】:2014-04-21 15:15:32
【问题描述】:

我正在尝试学习 JSONStore,在此过程中,我正在尝试执行一段代码,该代码将首先检查设备中是否已经存在特定的 JSONStore,并根据结果执行 if-else陈述。如果它不存在,它将创建一个并在其中添加一些数据。如果 jsonStore 已经存在,代码将用新数据替换以前存储的数据。但是当我尝试执行代码时,我的设备会显示 html 内容一段时间,然后屏幕变为空白。当我检查 logcat 时,我没有得到我在代码中添加的任何控制台日志语句。谁能帮助我理解这种行为以及可以做些什么来达到要求。

     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);
                        });                                      


               }

    });
});
}

【问题讨论】:

    标签: android ibm-mobilefirst jsonstore


    【解决方案1】:
    1. 您需要先初始化,否则WL.JSONStore.get(collectionName) 将始终返回未定义。如果您从未初始化 JSONStore,则无法使用它。
    2. replace API 适用于 JSONStore 文档(具有 _idjson 键的对象)。
    3. 你只需要一个.fail,错误对象会告诉你错误的来源(errorObject.src)。

    请参阅下面的未经测试的伪代码以了解您想要做什么:

    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);
    });
    
    }
    

    第一次执行时的预期输出:

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

    第一次执行以外的预期输出:

    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}}] 
    

    【讨论】:

    • 完美回复...将其添加到我最喜欢的问题中
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-04-11
    • 2012-04-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多