Undefined variable exception when assigning to a .first() query with no result

We are getting an undefined error exception when assigning a .first() query that return no results. The issue is related to the return logic in the first() query function in the QuickBuilder.cfc (line: 740). It returns a javaCast null if the query result is empty. According to the Adobe CF documentations, unexpected results will occur if you assign a javacast null to a variable. Our suggestion is to return the empty attribute to avoid this unexpected behavior. That is logic used in the qb orm library that Quick is based on.

Best regards and thanks in advance!

https://helpx.adobe.com/coldfusion/cfml-reference/coldfusion-functions/functions-in-k/javacast.html

1 Like

We will usually use FirstOrFail()

That means we don’t need to do the null check, we just catch the error.
You can do a .count() first but usually a firstOrFail() or a .first() with an

var result = myQuery.first();
if( isNull( result ){
   return "this error text";
}

Hi, @Pele_Santos! Welcome!

This is highly unlikely to change. It is clearly documented in the docs.. It has been this way for many major versions of Quick. To change now would not only be a breaking change but a major one for all users of Quick.

You can easily check for a result using isNull( entity ) or use one of the *orFail helper methods like firstOrFail and catch the exception. This has the added benefit of being able to provide a consistent response for all EntityNotFound exceptions.

Hope this helps and thanks for your post!

1 Like

Sorry for the late response. IsNull() does work if validated before assigning to a variable. My workaround was to validate before returning the result to another variable and avoid the exception. Thanks again!

1 Like