【问题标题】:IBM Worklight JSONStore | Remove Document from Collection and erase it from memoryIBM Worklight JSONStore |从集合中删除文档并将其从内存中删除
【发布时间】:2014-02-06 20:46:27
【问题描述】:

在添加新文档之前,我一直在尝试从集合和内存中删除所有文档。我通过以下方法尝试过

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

但是没有成功。它通过findAll显示所有文件

【问题讨论】:

    标签: ibm-mobilefirst jsonstore


    【解决方案1】:

    您可以使用removeCollection API 调用来删除集合中的所有文档,如果您想再次使用它,则必须重新初始化该集合。

    或者,请尝试以下示例:

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

    【讨论】:

    • 嘿!谢谢Cnandreu ...我通过使用removeCollection API解决了我的问题。
    猜你喜欢
    • 2015-02-09
    • 2013-09-04
    • 1970-01-01
    • 2014-08-16
    • 2017-04-11
    • 1970-01-01
    • 1970-01-01
    • 2014-05-20
    • 2014-10-19
    相关资源
    最近更新 更多