IBM Worklight JSONStore |从集合中删除文档并将其从内存中删除 [英] 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);
}
}
});
但未获得成功。它通过 findAll
But didn't get success. Its showing all documents by doing findAll
推荐答案
显示所有文件你可以使用< a href =http://pic.dhe.ibm.com/infocenter/wrklight/v6r1m0/topic/com.ibm.worklight.apiref.doc/html/refjavascript-client/html/JSONStoreInstance.html#removeCollection> removeCollection API调用以删除集合中的所有文档,如果要再次使用它,则必须重新初始化该集合。
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());
});
}
这篇关于IBM Worklight JSONStore |从集合中删除文档并将其从内存中删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!