0

I am using cf search (or rather the script based alternative in Railo) to do a basic keyword search, but I am getting an error when no criteria is entered.

What I would rather like to get back is an empty query object. Is this possible?

enter image description here

I can do checks on the string length etc using len(), but that would mean I never get the query object returned, which is what I would like.

Am I missing something here?

Thanks, Mikey.

Michael Giovanni Pumo
  • 14,338
  • 18
  • 91
  • 140
  • 3
    You can create an empty query object with [QueryNew](http://railodocs.org/querynew) - what are you trying to do? – Peter Boughton Aug 24 '13 at 20:57
  • I am using ajax to fetch back a query that is structured into JSON. Because the request is made on key up, there are chances where the argument supplied is blank / empty which causes this 500 error. I could do more checks in the JS itself I guess, but I'd always like to safeguard it as much as I can with predictable data returned. The error itself seems rather strange regardless though - any idea why it happens? – Michael Giovanni Pumo Aug 26 '13 at 12:10
  • Do a len check in JS and don't send the request unless it is (at least) 2 or more characters long. Don't just respond to CF errors with blank search results - display a suitable error message. – Peter Boughton Aug 26 '13 at 12:18
  • Why the error itself is happening is explained by **reading the error message**, which looks to be self-explanatory. _(If you can demonstrate that the error message is not correct then [raise an issue in Jira](https://issues.jboss.org/browse/RAILO).)_ – Peter Boughton Aug 26 '13 at 12:20
  • I understand why the error happens, it can't handle an empty criteria, which is obvious. But, it seems odd to me that it would behave this way. That's all I was stating. – Michael Giovanni Pumo Aug 26 '13 at 18:20

1 Answers1

0

As Peter said, as simple as:

<cftry>
    <cfsearch attributes>
<cfcatch type="application">
    <cfset MyQuery = QueryNew('column1','column2','column3')>
</cfcatch>
</cftry>

OR

<cfif len( attributes.searchCriteria )>
    <cfsearch attributes>
<cfelse>
    <!--- return blank query object --->
</cfif>
BKK
  • 2,073
  • 14
  • 17
  • I have thought about doing this, but it just seemed a bit...dirty. I will give it a go though and perhaps I'll get over myself :) thanks. Any idea why it returns an error at all though? Can tou not supply a blank criteria? Seems a bit of an odd error. – Michael Giovanni Pumo Aug 26 '13 at 12:06
  • Seems perfectly valid to me, you're searching for "nothing"? What is it supposed to rank the results by? A more elegant approach would be to just test for len() of the passed in argument. – BKK Sep 07 '13 at 03:39