Pregunta

Tengo el siguiente Applicaton.cfc

<cffunction name="onApplicationStart" access="public" returntype="Object">
 <cfset application.dsn = "myDB" />
 <cfset application.userGateway = createObject("component","cfc.UserGateway").init(dsn = application.dsn) />
 <cfreturn this />
</cffunction>

Este es mi componente UserGateway.cfc

<cfcomponent name="UserGateway" hint="Data Access Object" output="false">
 <cffunction name="init" access="public" hint="constructor" output="false" returntype="UserGateway">
  <cfargument name="dsn" type="string" required="true" hint="datasource" />
   <cfset variables.dsn = arguments.dsn />
 <cfreturn this />
 </cffunction>

 <cffunction name="getUsers" access="public" output="false" returntype="query">
  <cfargument name="id" type="String" default="" />
  <cfargument name="name" type="String" default="" />
  <cfargument name="district" type="String" default="" />
  <cfset var qQuery = "" />
  <cfquery name="qQuery" datasource="#variables.dsn#">
    SELECT *
    FROM A INNER JOIN B
    ON A.X = B.Y
    WHERE 0=0
    <cfif "#arguments.id#" neq "">
     AND B.X LIKE '%#arguments.id#%'
    </cfif>
    <cfif "#arguments.name#" neq "">
     AND (A.I LIKE '#arguments.name#%'
      OR A.J LIKE '#arguments.name#%')
    </cfif>
    <cfif "#arguments.district#" neq "">
     AND A.O LIKE '%#arguments.district#%'
    </cfif>
  </cfquery>
  <cfreturn qQuery />
 </cffunction>
</cfcomponent>

Y este es mi same.cfm

<cfform action="same.cfm" method="post" preservedata="true">
 <p>ID: <cfinput type="text" name="id" size="20" maxlength="4" /></p>
 <p>Name: <cfinput type="text" name="name" size="20" maxlength="64" /></p>
 <p>District: <cfinput type="text" name="district" size="20" maxlength="3" /></p>
 <p><cfinput class="button" type="submit" name="submit" value="OK" /></p>
</cfform>

<cfif IsDefined("form.submit")>
 <table>
  <cfset qQuery = application.userGateway.getUsers(id = form.id, name = form.name, district = form.district) />
  <cfoutput query="qQuery">
   <tr>
    <td>#qQuery.currentRow#.</a></td>
    <td>#qQuery.I#</a></td>
    <td>#qQuery.M#, #qQuery.N#</a></td>
    <td>#qQuery.D#</a></td>
   </tr>
  </cfoutput>
 </table>
</cfif>

Me sale el siguiente error:

Element USERGATEWAY is undefined in a Java object of type class [Ljava.lang.String;.
The error occurred in same.cfm: line 10

¿Qué me estoy perdiendo?

-------------------------------------------
-------------------------------------------

Cuando lo hago de esta manera funciona. debe ser algo trivial que yo como principiante no entiendo.

Application.cfc

<cffunction name="onRequestStart" access="public" returntype="String">
 <cfset request.dsn="myDB" />
</cffunction>

same.cfm

    <cfset userGateway = createObject("component","cfc.UserGateway").init(dsn = request.dsn) />
    <cfset qGetUser = userGateway.getUsers(id = form.personid, name = form.name, district = form.district) />
  <cfoutput query="qQuery">
   <tr>
    <td>#qQuery.currentRow#.</a></td>
    <td>#qQuery.I#</a></td>
    <td>#qQuery.M#, #qQuery.N#</a></td>
    <td>#qQuery.D#</a></td>
   </tr>
  </cfoutput>
¿Fue útil?

Solución

Hay dos cosas que veo mal aquí:

Primero, según tengo entendido, el uso del alcance 'this' en application.cfc no funciona de la forma en que intenta hacerlo. Al establecer su objeto userGateway en un valor de ámbito de aplicación, se vuelve globalmente disponible y realmente hace innecesario devolverlo en onApplicationStart. En su application.cfc, cambie su tipo de retorno a booleano y simplemente devuelva verdadero; eso debería solucionar su problema.

Segundo, si en su consulta, sus argumentos y condicionales no son indicadores de lo que realmente tiene, está haciendo referencia a un argumento 'personid' que no existe en su función. Al llamar a esa consulta a través de una llamada a un objeto en el ámbito de aplicación, he visto que el error de cadena de Java se devuelve como un error antes en lugar del error 'Variable no existe' de CF Friendly.

Otros consejos

En same.cfm, ejecute esto:

<cfset OnApplicationStart()>

Luego actualice la página nuevamente. ¿Funciona ahora?

<cffunction name="init" access="public" hint="constructor" output="false" returntype="UserGateway">

debería ser:

<cffunction name="init" access="public" hint="constructor" output="false" returntype="Any">

La siguiente línea es incorrecta:

<cfset application.userGateway = createObject("component","cfc.UserGateway").init(dsn = application.dsn) />

Debería leerse sin & "; cfc. &"; al comienzo del nombre del componente que desea:

<cfset application.userGateway = createObject("component","UserGateway").init(dsn = application.dsn) />

Además, verifique que el resto de la aplicación.cfc sea correcta porque algo no funciona correctamente, ya que debería haber visto este error que no pudo encontrar el componente cfc.UserGateway.

EDITAR: También olvidé mencionar que onApplicationStart no necesita devolver nada. El tipo de retorno debe ser nulo y no debe estar presente <return this/>.

reiniciar su servicio de CF podría ayudar.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top