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

Variable "not defined"? It's being set and incremented..

LEGEND ,
Aug 12, 2009 Aug 12, 2009

Copy link to clipboard

Copied

Hello, everyone.

I've got a real confusing issue.  I have one page that grabs some data from the database, then loops through seven included files in the CFOUTPUT.  One of the seven includes is setting a variable at the top of the page called "confirmed" to zero (0), has its own loop where it increments the value by one under certain conditions, then displays the value after its own loop is done.

The problem is that I am occasionally getting an error message that says "confirmed" is undefined (it happens 3 out of 5 times.)  How can this be?  Is there any way that anyone can think of that would cause this variable to disappear during processing?

Thanks,

^_^

Views

1.1K

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
Engaged ,
Aug 12, 2009 Aug 12, 2009

Copy link to clipboard

Copied

I can't really visualize from your description what sort of madness you might have constructed.   But <cfdump> and the IsDefined function can be your best friends in this universe.

If you're going to be working with any variable across several included files, probably the best way to handle it would be to put these variables into a struct which you initialize at the top of the routine.  (The reason for doing this is simply that the struct becomes a clear and obvious "name space" which makes it very obvious ... to you and to ColdFusion ... exactly what you are talking about.  e.g. "I'm not talking about any ol' xyzzy ... I'm talking about the xyzzy that's found in the structure named Y2."

"Nothing happens here."

It is now pitch dark.  If you continue, you might get eaten by a grue.

>

This will help to take care of any unwanted typos, and it also gives you a great opportunity to prevent any issues of "undefined values."  You initialize the structure exactly the way you want it.  And because you are now making explicit references to that structure throughout your code, any "oopsie!"s in your code will now stand out like a sore thumb.

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
LEGEND ,
Aug 12, 2009 Aug 12, 2009

Copy link to clipboard

Copied

Sorry.. I should have been more clear.  The variable is not being accessed by any of the other pages; the variable "confirmed" is used solely by the page that is creating it.

CFDUMP is a great troubleshooting tool, yes, if there are few iterations of a loop; if there is a lot of data, using CFDUMP in a loop of 3,500 iterations will slow processing down to a molasses crawl.  Sometimes, this module will iterate 22,000 to 37,000 times, depending.

IsDefined can prevent the module from breaking; but if the variable is not defined after a loop is done iterating - after it was defined at the beginning of the page (<cfset confirmed=0>) - then I am not sure what to do; I can't just arbitrarily throw a number into it, and the value that should be there is necessary to the process, not only for updating a log file, but also for displaying on the screen how many items were processed.

The variable is not being deleted inside the loop; the last line inside the loop is <cfset confirmed = val(confirmed) + 1>; so it's there for every iteration.  Somehow, after the loop is done and before <CFOUTPUT> displays it, it is removed.  The loop is inside a CFTRY/CFCATCH, no errors are being displayed prior to the module breaking.

So, I'm really scratching my head on this one.

^_^

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
Engaged ,
Aug 12, 2009 Aug 12, 2009

Copy link to clipboard

Copied

All that you know at this point is ... that you have reached a logical contradiction.  Therefore, one-or-more of your assessments of the present problem must be wrong; you just don't yet know which one.

Variables do not "disappear" by themselves; only when they pass out-of-scope.  (The relevant scopes here are Caller and Variable, and you should carefully review this concept in the documentation as it could well have some bearing on your problem.  It could, in fact, be your problem.)

When you realize that you are dealing with an apparent contradiction, you have to force yourself to systematically prove every single aspect of what you think that you are seeing.  You know that one-or-more of these assumptions are red herrings but you do not know which one.  You might have to do things like <cfdump>ing the entire Variable namespace, or inserting explicit debugging-tests which assert that such-and-so variable exists and fail if it doesn't.  You need to do this because, if you perceive that a variable is "disappearing," your counts cannot be relied-upon to be right.

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
LEGEND ,
Aug 12, 2009 Aug 12, 2009

Copy link to clipboard

Copied

I know variables don't disappear on their own.  Something is affecting it, I just don't know what; that's why I'm asking if anyone else knows of something that might cause this.

It's a short file, I'll include it, here.  I cannot see where "confirmed" is being removed.  It is used only in this page, nowhere else.

^_^

<cfsetting enablecfoutputonly="yes" showdebugoutput="yes" requesttimeout="86400">

<cfparam name="marketplace_module" default="AMAZON">

<cfparam name="marketplace_pk" default="">

<cfquery name="marketplaceParams" datasource="#data_source#">

<!--- GET DATA ON MARKET --->

</cfquery>

<cfwddx action="WDDX2CFML" input="#marketplaceParams.marketplaceData_wddx#" output="marketplaceData">

<cfset amazon_sellername = marketplaceData.account_sellername>

<cfset This_Host = Root_Host & "\" & marketplaceParams.clientid>

<cfset amazon_dir_local = This_Host & "\AMAZON\ref">

<cfset amazon_file = "ref_#DateFormat(Now(),'mmddyyyy')##TimeFormat(Now(),'HHmm')#.txt">

<cftry>

<cfquery name="order" datasource="#data_source#">

<!--- QUERY TO GET ORDERS --->

</cfquery>

<cfset confirmed = 0><!--- IT'S BEING INITIALIZED HERE --->

<cfif order.RecordCount>

<cfset content = "order-id order-item-id refund-amount reason message">

<cffile action="WRITE" file="#amazon_dir_local#\#amazon_file#" output="#content#" addnewline="Yes">

<cfoutput query="order">

<cfset refund_amount = DecimalFormat(unit_price+unit_shipping)>

<cfif unit_status EQ 9>

<cfset reason = "Inventory No Longer Available">

<cfelse>

<cfset reason = "Customer Return">

</cfif>

<cfset message = "">

<cfset content = order_id & CHR(9) & order_item_id & CHR(9) & refund_amount & CHR(9) & reason & CHR(9) & message>

<cffile action="APPEND" file="#amazon_dir_local#\#amazon_file#" output="#content#" addnewline="Yes">

<cfquery name="update" datasource="#data_source#">

<!--- QUERY TO UPDATE DATABASE --->

</cfquery>

<cfset notes = "Refund for Line #lineno# submitted to AMAZON.">

<cf_noteit clientid="1111" action="add" keytype="orderno" key="#orderno#" username="#userData.username#" notes="#notes#" show="0">

<cfset confirmed = val(confirmed) + 1><!--- BEING INCREMENTED HERE --->

<cfdump var="#confirmed#" expand="yes" label="confirmed value"><!--- JUST ADDED THIS PART --->

</cfoutput>

</cfif>

<cfcatch>

<cfoutput>#cfcatch.Message# - #cfcatch.Detail#</cfoutput>

<cfmail to="[my email address]" from="[server email address]" subject="An error">amazon.ref.exp.cfm has erred#chr(13)##chr(10)##cfcatch.Message# - #cfcatch.Detail#</cfmail>

</cfcatch>

</cftry>

<cfoutput>#val(confirmed)# orderitems refunded.<br/></cfoutput><!--- THIS IS WHERE CF TELLS ME IT'S NOT DEFINED --->

<cfif val(confirmed) gt 0>

<cf_logit siteid="123" username="#userData.username#" action="#val(confirmed)# AMAZON Orders refunded.">

<cffile action="WRITE" file="#amazon_dir_local#\#amazon_file#.flag" output="#amazon_file#" addnewline="No">

<cfoutput>File #amazon_file# written to #amazon_dir_local#.<br/></cfoutput>

</cfif>

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
Advisor ,
Aug 12, 2009 Aug 12, 2009

Copy link to clipboard

Copied

The variable confirmed is defined within a CFTRY block.  The error occurs when the variable is referenced outside the CFTRY block.  Try defining the variable before the opening CFTRY tag.

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
Engaged ,
Aug 13, 2009 Aug 13, 2009

Copy link to clipboard

Copied

LATEST

If an exception went off "at the right wrong time," it would not be evident because it would have been caught...  but the variable might not have gotten a chance to be defined yet.

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