Skip to main content
17-Peridot
December 9, 2022
Solved

Filter queries

  • December 9, 2022
  • 1 reply
  • 3454 views

Hello,

 

I have been struggling to get a function working where I check for if a filter query is coming into a service empty (no filter has been added).

 

jensc_0-1670575628976.png

 

First I thought it was just a normal JSON and tried to use .length().

But by doing so I receive this error message in the output:

jensc_1-1670575780415.png

I have also tried using isEmpty(), but receive the same error message as with .length().

So my only conclusion is that this type is not actually a JSON type.

 

Some other "issue" is that when logging out the value, it can be undefined OR just '{}', I guess depending on if we have had a filter after first load or not.

 

Any help with this would be much appreciated.

 

Regards,

Best answer by TravisPickett

Hi @jensc ,

 

Can you let me know what widget you are using in your mashup?  I did a test with the legacy data filter widget in 9.1.5 and the output in the Firefox debugger shows the JSON being properly formatted.

{"FilterQuery":{"filters":{"fieldName":"name","type":"LIKE","value":"test*"}}}

I also verified that the Query datatype should be JSON format and the 9.3 Composer enforces this structure when testing.

 

As for validating if the JSON is empty when properly formatted I have the following two solutions as the length method is not defined.

ThingWorx 9.1

let isEmpty = true;
for( let x in FilterQuery )
{
 isEmpty = false;
 break;
}

if(FilterQuery && !isEmpty)
{
 result = "Filter is not empty";
}
else
{
 result = "Filter is empty";
}

 

ThingWorx 9.3

if(FilterQuery && Object.keys( FilterQuery).length > 0)
{
 result = "Filter is not empty";
}
else
{
 result = "Filter is empty";
}

 

Thanks,

 

Travis

1 reply

1-Visitor
December 10, 2022

Hi

See if this helps:

var cities = "";
// Should be empty
if (cities.isEmpty()) {
    return "Empty";
} else {
    return "'" + cities + "'";
}

Regards

Pravin Pradhan

jensc17-PeridotAuthor
17-Peridot
December 12, 2022

Hello,

 

I'm not sure how this would help with checking if an input of type query is empty or not...

Isn't this just checking if the empty string "cities" is empty or not?

 

Regards,

12-Amethyst
December 13, 2022

If you try something like the following it should help with your initial null or undefined checks:

if( FilterQuery && FilterQuery.length() > 0 )
{
 // not empty, do processing
}

The other option is to explicitly check FilterQuery against null and undefined.  Your observations are correct the incoming value for FilterQuery depends alot on how the services is being called.

 

Thanks,

 

Travis