foundset.forEach() Continue Statement not Supported?

Questions, tips and tricks and techniques for scripting in Servoy

foundset.forEach() Continue Statement not Supported?

Postby kwpsd » Tue Apr 17, 2018 7:06 pm

Version: 8.2.3.3109

It appears that the Foundset Iterator loop structure does not support the continue statement to force advancement to the next record. When I attempt to use it, I get the following marker message:

The property continue is undefined for the type JSFoundset


So, how do you skip processing a record in the foundset.forEach() loop?

e.g. Say a record fails a validation test and you want to go to the next record:

Code: Select all
foundset.forEach(
            /**
             * @param {JSRecord} record
             * @param recordIndex
             * @param {JSFoundset} fs
             */
            function(record, recordIndex, fs)
            {
                if ( !validate( record ) foundset.continue

                ...
            }
        );



Thank you.
Kim W. Premuda
San Diego, CA USA
User avatar
kwpsd
 
Posts: 604
Joined: Sat Jul 28, 2007 6:59 pm
Location: San Diego, CA USA

Re: foundset.forEach() Continue Statement not Supported?

Postby Manolo_Etec » Wed Apr 18, 2018 12:19 pm

I solve this with a 'throw'

Code: Select all
         
try {
   foundset.forEach(
   /**
    * @param {JSRecord<db:/modulo/pro>} _rc
    * @param {Number} _index
    * @param {JSFoundSet<db:/modulo/prov>} _fs
    */
    function(_rc, _index, _fs){
       try {

          if ( !validate( record ))    throw "stop";
       } catch (e) {
          if(e != "stop"){
               application.output('HC_relProvListPrec_articulos ERROR: '+e,LOGGINGLEVEL.ERROR);
               throw e;
            }
       }
      
    });
} catch (e) {
   if(e != "stop"){
      application.output('HC_relProvListPrec_articulos ERROR: '+e,LOGGINGLEVEL.ERROR);
      throw e;
   }
}
Manuel López
Etec Programación SL
[email protected]
http://www.etecsoft.com

Servoy 8.2.2
Windows 10
Yesterday: Java 1.8.0_141-b15
User avatar
Manolo_Etec
 
Posts: 58
Joined: Fri Sep 12, 2008 3:58 pm

Re: foundset.forEach() Continue Statement not Supported?

Postby kwpsd » Wed Apr 18, 2018 7:56 pm

That's interesting...I would have never thought to use the error mechanism as a continue statement! I was hoping for a 'cleaner' solution but will give your method a try.

Thank you, Manuel.
Kim W. Premuda
San Diego, CA USA
User avatar
kwpsd
 
Posts: 604
Joined: Sat Jul 28, 2007 6:59 pm
Location: San Diego, CA USA

Re: foundset.forEach() Continue Statement not Supported?

Postby Ruben79 » Thu Apr 19, 2018 11:18 am

Since you are just running a function for each record, you can also just do a return:

Code: Select all
foundset.forEach(
            /**
             * @param {JSRecord} record
             * @param recordIndex
             * @param {JSFoundset} fs
             */
            function(record, recordIndex, fs)
            {
                if ( !validate( record ) return;

                ...
            }
        );
Ruben de Jong
Stb Software Development
SAN Partner

Stb Software Development - http://www.stb.nl
User avatar
Ruben79
 
Posts: 82
Joined: Wed Apr 18, 2007 12:43 pm

Re: foundset.forEach() Continue Statement not Supported?

Postby Manolo_Etec » Fri Apr 20, 2018 9:57 am

Sorry, I misunderstood you, I thought you wanted to break the foreach. As it says Ruben, with the 'return' you go straight to the next record.
Manuel López
Etec Programación SL
[email protected]
http://www.etecsoft.com

Servoy 8.2.2
Windows 10
Yesterday: Java 1.8.0_141-b15
User avatar
Manolo_Etec
 
Posts: 58
Joined: Fri Sep 12, 2008 3:58 pm

Re: foundset.forEach() Continue Statement not Supported?

Postby kwpsd » Sat Apr 21, 2018 11:07 pm

Forest through the trees! I was thinking in terms of a loop structure as opposed to a function call. The return is obvious, once you brought it to my attention.

Thank you both for responding!
Kim W. Premuda
San Diego, CA USA
User avatar
kwpsd
 
Posts: 604
Joined: Sat Jul 28, 2007 6:59 pm
Location: San Diego, CA USA

Re: foundset.forEach() Continue Statement not Supported?

Postby jcompagner » Tue May 01, 2018 4:44 pm

I think when you return something

like "return false"

then it will stop right away, for example if you would do something like

var recordFound = foundset.forEach(function(record){
if (record.xxx = "yes") return record;
});

then at the moment it hits a a record where "xxx" is "yes" it will stop and return that record.
Johan Compagner
Servoy
User avatar
jcompagner
 
Posts: 8086
Joined: Tue May 27, 2003 7:26 pm
Location: The Internet


Return to Methods

Who is online

Users browsing this forum: Bing [Bot] and 2 guests

cron