IBM Worklight JSONStore | Remove Document from Collection and erase it from memory

心已入冬 提交于 2019-11-27 08:44:05

问题


I have been trying to erase all document from collection and memory before adding new one. I tried it by following methods

1.

        WL.JSONStore.get("users").findAll().then(function (arrayResults){
            var options={push:true};
        if(arrayResults.length>0){
            for(var i=1;i<=arrayResults.length;i++){
            WL.JSONStore.get("users").remove(i,options);
            }
        }
            });

2.

            WL.JSONStore.get("users").findAll().then(function (arrayResults){
            var options={};
        if(arrayResults.length>0){
            for(var i=1;i<=arrayResults.length;i++){
            WL.JSONStore.get("users").erase(i,options);
            }
        }
            });

But didn't get success. Its showing all documents by doing findAll


回答1:


You can use the removeCollection API call to remove all docs in a collection, you will have to re-init the collection if you want to use it again.

Alternatively, try following this example:

function wlCommonInit () {

  WL.JSONStore.init({
    users: {
      name: 'string'
    }
  })

  .then(function () {
    return WL.JSONStore.get('users').add([{name: 'hello'}, {name: 'world'}]);
  })

  .then(function () {
    return WL.JSONStore.get('users').findAll();
  })

  .then(function (res){

    alert('Before - Docs inside:' + JSON.stringify(res));

    var ids = res.map(function(current){ return current._id  })

    var promises = [];

    while (ids.length) {
      promises.push(WL.JSONStore.get('users').remove(ids.pop()));
    }

    return WLJQ.when.apply(null, promises);
  })

  .then(function () {
    return WL.JSONStore.get('users').findAll();
  })

  .then(function (res) {
    alert('After - Docs inside:' + JSON.stringify(res));
  })

  .fail(function (err) {
    alert('Failed:' + err.toString());
  });
}


来源:https://stackoverflow.com/questions/21187290/ibm-worklight-jsonstore-remove-document-from-collection-and-erase-it-from-memo

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