0

我收到错误“transaction_types”未定义,并且无法理解原因。

我有application.cfc:

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

header.cfm 文件看起来像这样(每个文件都会调用 header,并且根据用户所在的目录有不同的子标题):

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

我遇到问题的目录有两个文件,index.cfm 和 subheader.cfm

subheader.cfm,第一行

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

index.cfm 的一部分,我认为问题可能是 cflocation,但我不确定:

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

--further down on this page, transaction_types is used 

我设置页面时认为 transaction_types 将在目录/index.cfm 加载的任何时候定义,因为应用程序文件总是加载 header.cfm 并随后在目录/index.cfm 之前加载目录/subheader.cfm。cflocation 会绕过这个吗?

4

1 回答 1

0

您在 OnRequest 中包含在variablesApplication.cfc 范围内设置变量的代码,然后稍后尝试在模板中引用。

一般而言,cfc 的变量范围,包括 Application.cfc,虽然是一种特殊情况,但不会传递到作为请求的一部分调用的模板。

如果您需要transaction_types在 Application.cfc OnRequest 期间设置,是否在包含的模板中,并稍后在 index.cfm 中引用,则应在类似范围内完成,然后再按此方式request引用。

子标题.cfm

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

然后参考request.transaction_typesindex.cfm 代码。

于 2020-07-15T12:07:56.003 回答