0
votes

I'm getting an error "transaction_types" is undefined, and having trouble understanding why.

I have application.cfc:

<cffunction name="onRequest" >  
    <cfargument name="targetPage" type="String" required=true/> 
    <cfinclude template="header.cfm"> 
</cffunction>

header.cfm file looks like this (header is called on every file and there is a different subheader depending on the directory the user is in):

<cfinclude template="#GetDirectoryFromPath(Arguments.targetPage)#subheader.cfm" />

The directory I'm having a problem with has two files, index.cfm and subheader.cfm

subheader.cfm, the first line

<cfset transaction_types = ["a", "b", "c"] /> 

part of index.cfm, and I think the issue might be the cflocation, but I'm not sure:

<cfif structKeyExists(url, "something") >
    -- some database work is done here --
    <cflocation url="index.cfm">
</cfif> 

--further down on this page, transaction_types is used 

I set the page up thinking transaction_types will be defined any time directory/index.cfm loads, since the application file always loads header.cfm and subsequently directory/subheader.cfm before directory/index.cfm. Does cflocation bypass this?

1
I believe you are correct Patrick. I don't think a cflocation call will go through the normal request processing. It does not call the onRequest method. They have documented that it behaves differently with the onRequestEnd method as it does not get called either BUT it does call the onAbort method instead. From the docs: When using cfabort, cflocation, or cfcontent tags, the OnAbort method is invoked instead on OnRequestEnd. The call to cflocation also stops processing of the current page. - Miguel-F
Having said all that I do believe the page you cflocation to should go through the normal request processing. So say you use cflocation to 'page2.cfm'. I think that page2.cfm will invoke the onRequest method but the initial page request is stopped when the cflocation tag fires. - Miguel-F
When the error occurs, what page is being processed - index.cfm or subheader.cfm? - Dan Bracuk
part of index.cfm, and I think the issue might be the cflocation... The index.cfm script is cflocating to itself ... is that a typo? FWIW, you can test whether cflocation calls OnRequest yourself. Create a separate Application.cfc and implement OnRequest. Inside OnRequest method display some text whenever the method is called, like: writeOutput("onRequest called"); Create a test cfm page with a cflocation call. Run it and see if "onRequest called" appears at the top of the screen. - SOS
AFAIK when the cflocation tag executes it stops the currently processing request, fires the onAbort method of Application.cfc, and then sends a 302 HTTP status code (by default) back to the user's browser. That in turn instructs the browser to make a new request of the URL included with that 302 status message. Which should start the process all over again to the new URL. - Miguel-F

1 Answers

0
votes

You are including code in OnRequest that is setting variables in variables scope of Application.cfc and then trying to reference in template later.

The variables scope of a cfc in general, including Application.cfc specifically though a special case, does not carry through to the templates that are called as part of the request.

If you need to set transaction_types during Application.cfc OnRequest, in included templates or not, and reference in index.cfm later, then it should be done in scope like request and then referenced later as such.

subheader.cfm

<cfset request.transaction_types = ["a", "b", "c"] />

Then refernece as request.transaction_types in index.cfm code.