Patrick Nanry
Posts: 0
Joined: Sat May 30, 2015 1:50 am

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

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

Shaun7189763
Posts: 0
Joined: Sun Oct 26, 2014 8:16 am

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

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

Serhii Kulibaba
Posts: 150
Joined: Tue Aug 27, 2013 1:47 pm

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

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

Patrick Nanry
Posts: 0
Joined: Sat May 30, 2015 1:50 am

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

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

Illya Stepanov
Posts: 0
Joined: Mon Mar 18, 2013 8:48 am

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

Hi Patrick -

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

Return to “Issues”