Skip to main content
16-Pearl
Sep 29, 2020 3:12 PM UTC
Question

Code inspector warning with "for each"

I have this warning when I use "for each" statement

iguerra_0-1601392092177.png

An old thread related to TW 8.3 was telling it will be fixed with future release, I have TW 8.5.6 but warning is still present.

Can this be fixed ?

 

5 replies

Support
Oct 6, 2020 3:35 PM UTC

Hi @iguerra.

 

Can you provide more info on what you're trying to do?  There may be an alternative approach.

 

Otherwise, we recommend posting your suggestion on the ThingWorx Ideas page.  This will allow other community members to vote for it, which will increase it's chances of being adopted for a future release.

 

Regards.

 

--Sharon

 

 

Translate
iguerra16-PearlAuthor
16-Pearl
Oct 6, 2020 4:02 PM UTC

Hello @slangley 

Just a loop, iterating infotable data (the sample I wrote is just to show the warn message, no code in the loop)

For-each works very well, it has no problems, code inspector  just shows the warning on the syntax that is instead correct. IMHO code inspector shows a wrong warning.

 

I know I can use a classic for loop with an index, but for-each is easier/beautiful/clean because doesn't need use row_length, an index variable, and also an object variable to point easily to the row data (instead of always write infot.rows[index]).

 

Translate
16-Pearl
Oct 9, 2020 3:18 PM UTC

I am afraid the warning is legit. I am running Thingworx 8.4.4 on Chrome 85.0.4183.121. For each produces a warning in design time and is completely ignored in runtime. No exception or anything, just ignored.

 

Here I get 1 log entry (yes, the DataTable does have records):

var entries = me.GetDataTableEntries({maxItems: 9999999});
logger.debug("Start for each with entries: " + entries.length);
for each (var entry in entries) {
 logger.debug("entry: " + entry);
}

 

Here I get 1+n entries:

var entries = me.GetDataTableEntries({maxItems: 9999999});
logger.debug("Start for each with entries: " + entries.length);
for (var i=0; i<entries.length; ++i) {
 var entry = entries[i]; 
 logger.debug("entry: " + entry);
}

 

Translate
iguerra16-PearlAuthor
16-Pearl
Oct 10, 2020 6:19 AM UTC

With for-each I need to use infotable.rows, like

for each (var entry in entries.rows) {

differently it will loop just one time with the whole (and single) infotable object.

instead specifiying .rows it will loop all records (it's an array).

try ...

 

 

 

Translate
16-Pearl
Oct 12, 2020 2:46 AM UTC

Absolutely makes sense. Tested immediately and had success. Thank you.

Translate