【问题标题】:CRUD Operation in JSONStore using MobileFirst Platform 7.1使用 MobileFirst Platform 7.1 在 JSONStore 中进行 CRUD 操作
【发布时间】:2016-04-27 04:38:48
【问题描述】:

我是 MFP 的新手,我正在尝试执行基本的 CRUD 操作。执行以下代码后什么都没有发生。如果我能得到一些帮助,我将不胜感激。谢谢。

ma​​in.js

function wlCommonInit () {

var collections = {
          people : {
            searchFields: {name: 'string', age: 'integer'}
          }
        };

        WL.JSONStore.init(collections).then(function (collections) {
          // handle success - collection.people (people's collection)
        }).fail(function (error) {
            alert("alert" + error);
          // handle failure
        });


        var collectionName = 'people';
        var options = {};

        var data = {name: 'yoel', age: 23};

        WL.JSONStore.get(collectionName).add(data, options).then(function () {
         // handle success
        }).fail(function (error) {
         // handle failure
        });




        // to display results using query yoel
        var query = {name: 'yoel'};

        var collectionName = 'people';

        var options = {
          exact: false, //default
          limit: 10 // returns a maximum of 10 documents, default: return every document

        };

        WL.JSONStore.get(collectionName).find(query, options).then(function (results) {
          // handle success - results (array of documents found)
        }).fail(function (error) {
          // handle failure
        });         
}//end wlCommonInit

【问题讨论】:

  • 日志中有任何错误消息吗?

标签: ibm-mobilefirst jsonstore


【解决方案1】:

JSONStore 是异步的。使用您编写的代码,您无法确定它的运行顺序。

JavaScript 代码很可能在您的 init() 发生之前调用您的 add()find() 之一。

【讨论】:

    【解决方案2】:

    我建议您不要在 wlCommonInit 中编写代码,因为 JSONStore 可能尚未加载。您可以尝试将其与按钮按下之类的事件联系起来,或者将其放入一个函数中,然后在控制台中调用它。此外,就像@Chevy Hungerford 所说,JSONStore 是异步的,所以通过链接来利用承诺。

    var collections = {
          people : {
            searchFields: {name: 'string', age: 'integer'}
          }
        };
    // to display results using query yoel
    var query = {name: 'yoel'};
    
    var options = {
          exact: false, //default
          limit: 10 // returns a maximum of 10 documents, default: return every document
    
        };
    
    var collectionName = 'people';
    var data = [{name: 'yoel', age: 23}]; //best if the data is an array
    
        WL.JSONStore.init(collections).then(function (collections) {
          // handle success - collection.people (people's collection)
              return WL.JSONStore.get(collectionName).add(data);
        })
        .then(function (res){
               return WL.JSONStore.get(collectionName).find(query, options)
        })
        .then(function (res){
           //handle success - getting data
        })
        .fail(function (error) {
            alert("alert" + error);
          // handle failure
        });
    

    【讨论】:

      猜你喜欢
      • 2016-07-19
      • 2017-08-17
      • 1970-01-01
      • 2015-09-04
      • 2023-03-05
      • 2015-11-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多