Page 1 of 1

In Server Code: How to iterate through each entry in a collection to test if a field matches a certain string

Posted: Sat May 30, 2015 1:58 am
by Patrick Nanry

I am trying to iterate through a collection and test whether one of the fields for each entry in the collection matches a certain string. if so, i would like to delete this entry from database. what I am mainly having trouble with is the syntax used in server code order to call a delete function on an entry in a collection in a database.

the pseudocode for what i am attempting to describe:

for(each entry i in the database) {
if(i.field == "some string") {
database.delete(i);
}
}


In Server Code: How to iterate through each entry in a collection to test if a field matches a certain string

Posted: Sat May 30, 2015 7:11 am
by Shaun7189763

Hi Patrick,

You could add a 'Run javascript" action on the success event of the collection you iterate through and add a code as follows:

$.each(data, function( index, value ) {

Code: Select all

if (value.yourField == "some string" ) { 

      serviceToDeleteinDB.execute({ 
            data:{ 
              'where':{'field':index} 
             }, 
             headers:{}, 
             success: function(e) { 
              } 
      }); 

}
});

Hope this will get you started.

Shaun


In Server Code: How to iterate through each entry in a collection to test if a field matches a certain string

Posted: Sat May 30, 2015 9:47 am
by Serhii Kulibaba

Hello Patrick,

Please use for (item in arr) for getting all items, and use function splice() for deletion array items.

E.g JS code:

prevar arr = [1,2,3,4];

for (item in arr){ // each item from array
if (arr[item] === 2){
arr.splice(item,1); // remove array item
}
}
console.log(arr);/pre


In Server Code: How to iterate through each entry in a collection to test if a field matches a certain string

Posted: Sat May 30, 2015 4:05 pm
by Patrick Nanry

My main confusion is just how to make a call to the database using the server code. So, what is the syntax used to invoke one of the methods in the below link? https://devcenter.appery.io/documenta...


In Server Code: How to iterate through each entry in a collection to test if a field matches a certain string

Posted: Sat May 30, 2015 11:31 pm
by Illya Stepanov

Hi Patrick -

Below in this article is section with code examples like: emQueries example/em, or what syntax do you mean?