cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Community Tip - Your Friends List is a way to easily have access to the community members that you interact with the most! X

Filter queries

jensc
17-Peridot

Filter queries

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,

1 ACCEPTED SOLUTION

Accepted Solutions

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

View solution in original post

8 REPLIES 8

Hi

See if this helps:

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

Regards

Pravin Pradhan

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,

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

Hello,

 

I have kind of tested this before, but ran it again just to make sure. 

My service looks like this:

 

jensc_0-1670941516067.png

 

Running this with this input:

 

jensc_1-1670941558128.png

 

The input I get from the payload in F12:

 

jensc_2-1670941591832.png

 

This still gives me the same "error".

 

jensc_3-1670941634573.png

I'm not sure, maybe the data in the payload is not correct?

 

I am running this on a 9.1.12 platform, so maybe this also plays a role.

 

Thanks,

 

Hi @jensc 

 

Yes, there appears to be an issue with your payload.  You will need double quotes around you keys in the JSON, 

{ "filters":{"fieldName": "Machine", "type": "LIKE", "value":"3*"}}

I missed the original type of your input being set to Query and not JSON for the service.

 

If you have the input set to JSON, Composer will flag the input as incorrect format, but for some reason when it is set to Query it does not.

 

Thanks,

 

Travis

Hello @TravisPickett ,

 

Yes, I noticed that it is not a "correct" JSON format, but I'm not sure if the "query" input type is supposed to be of "JSON" format.

The payload is what I get from using the advanced data filter:

 

jensc_1-1670943998712.png

 

And it does work with the query intotable function.

var param = {
	query: FilterQuery,
	t: allData
};

resultInfo = Resources["InfoTableFunctions"].Query(param);

 

So if it comes in as "almost" a JSON object, how could I check if it is empty or not? 

I can't seem to use the JSON.stringify() on it, nor the .toString().

 

I have had another forum post in regards to JSON input variables before and was told it is a Java JSON wrapper and not a JS JSON.
Maybe this is similar?

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

Hello @TravisPickett 

 

Thank you for your reply. I'll try to answer all of your points.

 

For what widget I am using, I am using this:

jensc_0-1671032803526.png

 

I assume it is this extension:

jensc_1-1671032835436.png

 

I am on this version of TWX:

jensc_2-1671032866475.png

 

This is the payload in Chrome developer tool:

jensc_0-1671033219523.png

 

I suppose it could be that this is just a bug in this version.

We are upgrading to a 9.3 instance soon so I will try it out again when we have migrated.

 

Thank you very much for your help!

Top Tags