Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
378 views
in Technique[技术] by (71.8m points)

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

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...