Page 1 of 1

How to check if REST Service results are empty, the proper way.

Posted: Tue Feb 04, 2014 9:48 pm
by Nathan Friend

To check if the response from a REST service request is empty you need to use two different methods. You can not be sure that a call to (data.length == 0) will not fail. The most valid way to check the response is to also check if it is null.

In it's simplest form, you would want to do the following:
span
if(data == null || data.length < 1){
// run your function
}
/span

However not all services will produce an empty response in the data array when no records are found. You will need to make sure that the array you are checking actually returns empty in the event there are no records returned. Below is an example of a response from Google that returned no records.

Google Places API call example:

span
{

Code: Select all

"html_attributions" : [ 

 ], 

"results" : [ 

 ], 

"status" : "ZERO_RESULTS" 

}
/span

As you can see there is data being returned in the data array but the results array is empty, which in this case the (data.length) or (data == null) both would return false because the data array is not empty.

You would need to change your code to the following:

span
if(data.results == null || data.results.length < 1){
// run your function
}
/span

With the Google Places response example above you could also do it this way:

span
if(data.status == 'ZERO_RESULTS'){
// run your function
}
/span

I hope this helps. Feel free to ask me questions, I enjoy helping where I can.


How to check if REST Service results are empty, the proper way.

Posted: Tue Feb 04, 2014 9:51 pm
by maxkatz

Thanks for sharing.. I will add link to our docs.


How to check if REST Service results are empty, the proper way.

Posted: Thu Jan 01, 2015 10:20 pm
by Istvan

Hi, im a beginer...
I try it in complete case but dosnt work. "Data not definied"
How can I check the query resultes?

Thanks?


How to check if REST Service results are empty, the proper way.

Posted: Fri Jan 02, 2015 2:45 am
by Yurii Orishchuk

Hello,

Unforunatly it's not clear your problem this time.

Please show us your implementation screen shots and where you have this "Data not defined".

Regards.


How to check if REST Service results are empty, the proper way.

Posted: Fri Jan 02, 2015 10:25 am
by Istvan

How to check if REST Service results are empty, the proper way.

Posted: Fri Jan 02, 2015 4:18 pm
by Istvan

My plan is

i would like check the resultes in an query service when complate. If empty then i would like run a create service and this is complate (after create is important) run again the query service.

But when is wnat to see the result ... data is not defined.


How to check if REST Service results are empty, the proper way.

Posted: Fri Jan 02, 2015 8:50 pm
by Istvan

I moved to success ... and working. Im sorry. It was a user error :-)


How to check if REST Service results are empty, the proper way.

Posted: Fri Jan 02, 2015 8:57 pm
by Evgene Karachevtsev

Hello Istvan,

Thank you for the update, glad it works!