• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

returning function value

Guest
Dec 13, 2007 Dec 13, 2007

Copy link to clipboard

Copied

this is my .cfc
<cfcomponent>
<cffunction name="httpTransaction" access="public"
returntype="string">
<cfargument name="USER" type="string" required="yes">
<cfargument name="VENDOR" type="string" required="yes">

<CFHTTP url="" method="post" resolveurl="no" timeout="30"
<CFHTTPPARAM type="header" name="X-VPS-REQUEST-ID"
value="#CreateUUID()#">
<CFHTTPPARAM type="header" name="X-VPS-CLIENT-TIMEOUT" value="30">
<CFHTTPPARAM name="USER" type="formfield" value="#USER#" encoded="no">
<CFHTTPPARAM name="VENDOR" type="formfield" value="#VENDOR#"
encoded="no">
</CFHTTP>


<!--- how do i return the value --->


</cffunction>
</cfcomponent>


and I'm trying to pass the


<cfoutput>CFHTTP.FileContent#</cfoutput>to the page that is invoking
the above function.
Thanks,
TOPICS
Advanced techniques

Views

680

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Participant , Dec 14, 2007 Dec 14, 2007
Look -- your httpTransaction() function is returning a string. That string is referenced in your code as 'myResult'. Therefore, just change this:
<CFSET content = #myResult.fileConent#>
<CFOUTPUT>#content#</CFOUTPUT>

to this:

<cfoutput>#myResult#</cfoutput>

Votes

Translate

Translate
LEGEND ,
Dec 13, 2007 Dec 13, 2007

Copy link to clipboard

Copied

RTFM

<cffunction ...>
<cfargument1 ...>
<cfargument2 ...>
....
<cfset var myResult = "">
...
<cfreturn myResult>
</cffunction>

hth

---
Azadi Saryev
Sabai-dee.com
http://www.sabai-dee.com

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 13, 2007 Dec 13, 2007

Copy link to clipboard

Copied

I think it's only appropriate to return the cfhttp entity that Coldfusion creates after running the tag. I think its a structure, but can't recall now. So I use type "any". The result is:

<cffunction name="httpTransaction" access="public"
returntype="any" output="false">

<!--- ... etc., etc., ... --->

<cfreturn cfhttp>
</cffunction>

On the calling page you would then have:
<cfset httpObj = createobject ("component", "component_path")>
<cfset myResult = httpObj.httpTransaction(myUser, myVendor)>
<cfset content = myResult.fileContent>

P.S.
- give url attribute a value;
- scope your vars, hence arguments.USER and arguments.VENDOR

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Dec 14, 2007 Dec 14, 2007

Copy link to clipboard

Copied

I'm a little confused on:
<cfset myResult = httpObj.httpTransaction(myUser, myVendor)>

are the variables "myUser", "myVendor", coming from the scope of the values that we've already passed to the cfarguments? I'm wondering if we are passing those same variables again?

thanks,

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Dec 14, 2007 Dec 14, 2007

Copy link to clipboard

Copied

Wait. I think I spoke too soon. I'll try something. It's usually after you move the piece that you see it's a bad move.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Dec 14, 2007 Dec 14, 2007

Copy link to clipboard

Copied

OK, it all made sense, but I have one last question. I'm getting this error message,

"You have attempted to dereference a scalar variable of type class java.lang.String as a structure with members".

He is what we put together:

<cfset requestData = StructNew()>
<CFSET requestData.VENDOR = "john">
<CFSET requestData.USER = "doe">

<CFSET httpObj = createObject("component", "Caller")>
<CFSET myResult = httpObj.httpTransaction(requestData.VENDOR,requestData.USER )>
<CFSET content = #myResult.fileConent#>

<CFOUTPUT>#content#</CFOUTPUT>

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Dec 14, 2007 Dec 14, 2007

Copy link to clipboard

Copied

Look -- your httpTransaction() function is returning a string. That string is referenced in your code as 'myResult'. Therefore, just change this:
<CFSET content = #myResult.fileConent#>
<CFOUTPUT>#content#</CFOUTPUT>

to this:

<cfoutput>#myResult#</cfoutput>

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Dec 14, 2007 Dec 14, 2007

Copy link to clipboard

Copied

You rock. Thanks for the inspiration!!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 15, 2007 Dec 15, 2007

Copy link to clipboard

Copied

LATEST
Error message:
"You have attempted to dereference a scalar variable of type class java.lang.String as a structure with members".

I advised you to return the struct cfhttp, but you continued to return a string instead. Hence the error.

I think it's much better for your code to return the cfhttp structure rather than a string. Just change the return type to "struct" or to "any". Then let the function end with: <cfreturn cfhttp>.

There are at least two good reasons to return a structure. First, you thereby make much more information available to the caller. Secondly, it enables the calling page to do some validation, for example, like this:

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources
Documentation