CRUD Operation in JSONStore using MobileFirst Plat

2019-09-10 00:50发布

I'm new to MFP and I'm trying to perform a basic CRUD operation. Nothing is happening after the following code is executed. I will highly appreciate if i can get some help. Thank you.

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

2条回答
迷人小祖宗
2楼-- · 2019-09-10 01:04

JSONStore is asynchronous. With the code you wrote you cannot be sure of the order it is run.

The JavaScript code is most likely calling one of your add() or find() before your init() happens.

查看更多
Animai°情兽
3楼-- · 2019-09-10 01:15

I would suggest you not writing the code within wlCommonInit because JSONStore may not be loaded yet. You could try tying it to a event like a button press or just put it into a function then call it in the console. Also, like @Chevy Hungerford has said, JSONStore is asynchronous so utilize the promises by chaining.

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
    });
查看更多
登录 后发表回答