0

I am getting this error on some code I inherited. I am running cf9 and jQuery 1.7.

The value + (indx + 1) + cannot be converted to a number.

This is the loop

    for (var indx = 0; indx < avgPopulationRecordsCount; indx++) {
    CloneAvgPopulationItem(indx, '#rc.qGetAP.ID[' + (indx + 1) + ']#');
    }
0

2 Answers 2

5

Your error message is entirely accurate: "The value + (indx + 1) + cannot be converted to a number."

Your variable rc.qGetAP.ID is a query column (effectively an array), and you are trying to index it with a string (as indicated by the single quotes), which CF is attempting to convert into a row number, but it cannot translate the text + (indx + 1) + into a number.

The problem here is less the code and more a matter of understanding what's going on:

JavaScript (which is what this is, not jQuery) occurs in the browser whilst ColdFusion occurs on the server.

These are two separate and distinct environments.

They are not interleaved in the way you are trying to do - they interact only over HTTP requests/responses (or via WebSockets/etc).

In brief:

  1. The browser asks for a page using a HTTP request.
  2. The CF server compiles and executes CFML code, which outputs text.
  3. That text is sent via HTTP response to the client.
  4. The browser then interprets that text as HTML/CSS/JS/etc - completely apart from the action of the CFML code being processed.

For a more complete explanation on client/server and HTTP request/response, here is a blog post discussing the subject.


Once you have understood how this process works, the solution is to either:

1) generate all the relevant JS variables from CF first (so your Ids are translated to a JS array), and you have a single line of code to call the JS function CloneAvgPopulationItem inside a JS loop.

For example:

<script type="text/javascript">
    var AvgPopIds = <cfoutput>#serialiseJson(ValueArray(rc.qGetAP.ID))#</cfoutput>;

    for (var index = 0; index < avgPopulationRecordsCount; index++) {
    CloneAvgPopulationItem(index, AvgPopIds[index]);
    }
</script>


2) resolve the loop on the CF side, so you get multiple JS lines calling the CloneAvgPopulationItem function.

For example:

<script type="text/javascript">
    <cfoutput query="rc.qGetAP">
        CloneAvgPopulationItem(#rc.qGetAP.CurrentRow#,'#JsStringFormat(rc.qGetAP.Id)#');
    </cfoutput>
</script>


It is important to state: do not just use these code examples but read and understand the article linked above about where JavaScript and ColdFusion run - the whole HTTP request/response process is an important one to understand and will avoid you encountering similar problems in future.

Sign up to request clarification or add additional context in comments.

1 Comment

I decided to return that query as json, that way it is all ColdFusion
0

It looks like you are mixing <cfscript> with Javascript. ColdFusion gets processed first on server, then the generated Javascript gets generated. It looks like what you want is closer to

<cfoutput>
<script type="text/javascript>


<cfloop from="0" to="#avgPopulationRecordsCount#" index="indx">
    CloneAvgPopulationItem(#indx#, '#rc.qGetAP.ID[indx]#');
</cfloop>

</script>
</cfoutput>

Update based on comments

After the above code is ran on the server, the following will on the web page.

<script type="text/javascript>

    CloneAvgPopulationItem(1, 'someInfo');
    CloneAvgPopulationItem(1, 'moreInfo');
    CloneAvgPopulationItem(1, 'yetmoreInfo');
    ...

</script>

2 Comments

Thanks, I do have script tags and cfoutput tags.
This answer misses the most important aspect of the problem - that you cannot mix CFML and JavaScript. You can use CFML to generate JavaScript, but they are executed completely independently. See my answer for a further explanation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.