CRUD Operation in JSONStore using MobileFirst Platform 7.1

柔情痞子 提交于 2019-12-12 02:41:07

问题


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

回答1:


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.




回答2:


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


来源:https://stackoverflow.com/questions/34915200/crud-operation-in-jsonstore-using-mobilefirst-platform-7-1

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!