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

前端 未结 1 1279
伪装坚强ぢ
伪装坚强ぢ 2020-12-12 05:56

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.ge         


        
相关标签:
1条回答
  • 2020-12-12 06:35

    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());
      });
    }
    
    0 讨论(0)
提交回复
热议问题