ibm-mobilefirst – IBM Worklight JSONStore |从集合中删除文档并将其从内存中删除

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

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显示所有文档

您可以使用 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());
  });
}

相关文章

AJAX是一种基于JavaScript和XML的技术,能够使网页实现异步交...
在网页开发中,我们常常需要通过Ajax从后端获取数据并在页面...
在前端开发中,经常需要循环JSON对象数组进行数据操作。使用...
AJAX(Asynchronous JavaScript and XML)是一种用于创建 We...
AJAX技术被广泛应用于现代Web开发,它可以在无需重新加载页面...
Ajax是一种通过JavaScript和HTTP请求交互的技术,可以实现无...