如何在APPLICATION范围内的同一个CFC中调用第二个函数?

Evi*_*mes 1 coldfusion cfc coldfusion-9

我正在使用ColdFusion 9.0.1.

首先让我说我可能没有问正确的问题.由于每个函数独立工作,只有当一个函数调用另一个函数时才会失败,我认为问题在于如何调用函数.

我正在创建一个包含结构的应用程序变量.该结构包含对象的引用,orders.cfc.

if (not isDefined("APPLICATION.AppInfo") or not isStruct(APPLICATION.AppInfo)) {
    APPLICATION.AppInfo = structNew();
    APPLICATION.AppInfo.objOrders = createObject("component", "globaladmin.orders");
}
Run Code Online (Sandbox Code Playgroud)

我能够成功访问orders.cfc中的方法,如下所示:

OrderItemList = APPLICATION.AppInfo.objOrders.orderItemList(URL.Customer);
Run Code Online (Sandbox Code Playgroud)

我在orders.cfc中有调用order.cfc中的其他方法的方法,有点像这样(为了简单而伪造):

<cffunction name="orderItemList">
    <cfscript>
          LOCAL.RandomNumber = getRandomNumber();
          return LOCAL.RandomNumber;
    </cfscript>
</cffunction>

<cffunction name="getRandomNumber">
    <cfscript>
        LOCAL.SomeNumber= randRange(0,10);
        return LOCAL.SomeNumber;
    </cfscript>
</cffunction>
Run Code Online (Sandbox Code Playgroud)

我收到此错误:

Entity has incorrect type for being called as a function. The symbol you provided getRandomNumber is not the name of a function.
Run Code Online (Sandbox Code Playgroud)

我想也许我不能在没有先创建对象的情况下在同一个CFC中引用一个函数,所以我这样做:

<cffunction name="orderItemList">
    <cfscript>
          LOCAL.RandomNumber = APPLICATION.AppInfo.objOrders.getRandomNumber();
          return LOCAL.RandomNumber;
    </cfscript>
</cffunction>
Run Code Online (Sandbox Code Playgroud)

然后,我会收到此错误:

Either there are no methods with the specified method name and argument types, or the method getRandomNumber is overloaded with arguments types that ColdFusion can't decipher reliably. If this is a Java object and you verified that the method exists, you may need to use the javacast function to reduce ambiguity.
Run Code Online (Sandbox Code Playgroud)

我应该如何在同一个CFC中调用第二个函数?

Jak*_*sel 6

我要尝试的第一件事就是在你的函数中改变所有变量:

<cffunction name="orderItemList">
    <cfscript>
          var RandomNumber = getRandomNumber();
          return RandomNumber;
    </cfscript>
</cffunction>

<cffunction name="getRandomNumber">
    <cfscript>
        var SomeNumber= randRange(0,10);
        return SomeNumber;
    </cfscript>
</cffunction>
Run Code Online (Sandbox Code Playgroud)

如果这不能解决问题,请告诉我,我们可以进一步探索.

编辑 好了,现在解决了本地范围问题,试试这个:

<cffunction name="orderItemList">
    <cfscript>
          LOCAL.RandomNumber = THIS.getRandomNumber();
          return LOCAL.RandomNumber;
    </cfscript>
</cffunction>

<cffunction name="getRandomNumber">
    <cfscript>
        LOCAL.SomeNumber= randRange(0,10);
        return LOCAL.SomeNumber;
    </cfscript>
</cffunction>
Run Code Online (Sandbox Code Playgroud)

  • 必须要进行其他操作,您不需要使用THIS在同一个CFC中调用函数. (5认同)