Skip to main content
Participating Frequently
October 6, 2011
Answered

return type is not of specified return type.... so how do you return a null

  • October 6, 2011
  • 1 reply
  • 1668 views

My problem seems to stem from when no results are returned, I would like to return the same object type but as a null or so I think that's what I would like to do. If someone could please shed some light on how I should return an object with null I would be most appreciative.

I would like to note I have already tried  returning entityNew("someObj") and that also resulted in failure.

<!---- get some object ---->

          <cffunction name="getSomeObject" returntype="someObj" access="remote" >

                    <cfargument name="client_key" required="true" type="string" >

                    <cfargument name="customer_id" type="string" required="true" />

 

                    <cfscript>

                              value = [];

                              value = entityload(" some_table",{client_key = "#client_key#",customer_id = "#customer_id#"});

 

                              if(arraylen(value) == 1)

                                        {

                                                  return value[1];

                                        }

                                        return;

                    </cfscript>

          </cffunction>

</cfcomponent>

thank you in advance!

    This topic has been closed for replies.
    Correct answer Sean Coyne

    I have tried casting to null in the same error is thrown by ColdFusion, I place the problem at ColdFusion because that's where the error is thrown.  The message in the error from CF is variable X is undefined.


    Are you doing something like <cfset x = javacast("null","") />??

    If so, then, again CF has no concept of null so you can't assign it to a CF variable.  if you need to return null, first change your returntype attribute to "any" and return it directly, <cfreturn javacast("null","") />

    1 reply

    Inspiring
    October 6, 2011

    First, ColdFusion variables never have a value of null.  That concept does not exist.  They can values like empty strings or empty structures, but they can't be null.

    Next, "someObj" is not a valid return type.  That's why you got the error message.  The documentation will tell you what return types are valid.

    Next, it appears that you are trying to insert a record into a database and at the same time, creating a variable named value.  I'm not familiar with that syntax, but does entity load return an array?  If so, what would be returned if the array length was 1?

    Not related to your question, but, you are asking for trouble by not using the keyword var to create variables inside your function.

    Participating Frequently
    October 6, 2011

    Thank you for your reply Dan,

    I can see I was not clear about the return type, the return type is a defined cfc that is a value object and is mapped to an action script class.

    so my question is if my query returns no results then what should I return to the flex client so it deserializes as a someObj that equates to null?

    Does this make more sense?

    Inspiring
    October 6, 2011

    I'm not a flex/flash programmer so some of this stuff is outside my area of knowlege.  Having said that, your code starts by creating an empty array.  What would happen if you simply returned an empty array to flex?