2016-01-21 84 views
0

我是MFP的新手,我试图执行基本的CRUD操作。下面的代码执行后没有任何事情发生。如果我能得到一些帮助,我将非常感激。谢谢。使用MobileFirst Platform 7.1在JSONStore中执行CRUD操作

main.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 
+0

来自日志的任何错误消息? –

回答

1

JSONStore是异步的。用你写的代码你不能确定它运行的顺序。

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

0

我建议你不要在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 
    }); 
相关问题