0

When I execute the code below, resources_gathered is a string, but I get the following error ('Rx222y280.....' is the string in the column resources_gathered):

visualize_resources.cfm?subject_id=395229:105 Uncaught ReferenceError: Rx222y208Rx224y208Rx224y210Rx244y231Rx246y231Rx246y233Rx233y244Rx217y243R is not defined

<cfquery name="getField" datasource="exmind" result="result">
    select resources_gathered, completed_fields from dbo.sf
    where subject_id=#subject_id#
</cfquery>  
<cfset record = getField>

resourcesGathered = JSON.stringify(eval(<cfoutput>#record.resources_gathered#</cfoutput>));
//resourcesGathered = (<cfoutput>#record.resources_gathered#</cfoutput>).toString();
alert(resourcesGathered);

I have tried formatting the string differently before it gets inserted into the table, but I get a reference error anytime I have to read it. Below you can see how I insert it into the table (all code below this point happens in another html file):

<cfquery datasource="exmind">
    update dbo.sf
    set resources_gathered = <cfqueryparam value="#resources_gathered#" 
    cfsqltype="cf_sql_nvarchar">
    where subject_id = #subject_id#
</cfquery>

The table is correct when I do this, so I am unsure what I need to change to be able to use this data in another html file. I am able to insert/select integers (such as completed_fields) which have been converted to strings, but not this more complex string. Below you can see how the string gets created. It is passed to the query html using a cfform.

for (i=0; i<resources.length; i++) {                      //resources is an array
    if (resources[i].visibility == true){
        resourcesFormString += "x";
        resourcesFormString += resources[i].x.toString();  //x and y are ints
        resourcesFormString += "y";
        resourcesFormString += resources[i].y.toString();
        resourcesFormString += "R";
    }
}

document.getElementById("resources_gathered").value = resourcesFormString;
  • Your intent is not clear. Are you looking to replace the existing value of resources_gathered with another version of the same value? – Dan Bracuk Aug 29 '22 at 20:04
  • I am looking to use the value of resourcesGathered in javascript. The string contains x and y coords I'd like to display on screen. – Bayley Sapara Aug 29 '22 at 20:07
  • Two ways of passing coldfusion variables to javascript are the `toScript()` function and the `` tag. Also, your update query might not be necessary. – Dan Bracuk Aug 29 '22 at 20:45
  • The update query is in another file, this data needs to be recorded to an sql table so it is necessary. The info needs to be displayed at a time after the data has been recorded. – Bayley Sapara Aug 29 '22 at 20:50
  • I don't see how wddx or toScript() will help. wddx appears to be for complex data strutctures (I have successfully converted my complex data into a string) and toScript() passes the variable to javascript. I do not need to do either of these things. The problem is that even though the variable has gone through JSON.stringify, it seems it is being treated as a variable name (which I obviously have not defined and do not want to) instead of a literal string – Bayley Sapara Aug 29 '22 at 20:59
  • 1
    What are you _hoping_ to result from `eval`-ing `"Rx222y208Rx224y208Rx224y210Rx244y231Rx246y231Rx246y233Rx233y244Rx217y243R"`? Do you understand what `eval` does? – Adam Cameron Aug 30 '22 at 19:57
  • Hang on. Do you understand that CFML executes on the CFML server, and JS executes on the browser, and there's no connection between the two? It's unclear from your question what you are actually wanting to achieve. This is old, but possibly might help your understand of how/when code executes: https://blog.adamcameron.me/2012/10/the-coldfusion-requestresponse-process.html – Adam Cameron Aug 30 '22 at 20:00
  • I undestand that they exist in different places, but I need to retrieve data from the database and use it in my JS code. My method above works for some columns but not others. Example, when I use the query above, I can retrieve the value of completed_fields, but resources_gathered throws an Uncaught Reference Error, even though I stored them both as strings (one nvarchar(50) and one nvarchar(max)). It does not make sense that the same approach would work for one column and not another. And, the script "knows" what I am trying to retrieve. (See the error message, the string starting with R) – Bayley Sapara Aug 30 '22 at 20:56
  • It looks like eval() is not the best thign to do, but I cannot use the 'Rx2...' string at all. In Javascript, I cannot even print it directly without getting the same Uncaught Reference Error. – Bayley Sapara Aug 30 '22 at 21:01
  • alert(typeof(#record.completed_fields#)); //=number alert(typeof(JSON.stringify(#record.completed_fields#))); //=string alert(typeof(JSON.stringify(eval(#record.completed_fields)))); //=string alert(typeof(#record.resources_gathered#)); //=undefined alert(typeof(JSON.stringify(#record.resources_gathered#))); //Uncaught Reference Error [string returned by cfoutput] is not defined alert(typeof(JSON.stringify(eval(#record.resources_gathered#)))); – Bayley Sapara Aug 30 '22 at 21:02
  • I believe I have solved this issue by doing the query in one html page, and then passing the values by form to be accessed by a second page – Bayley Sapara Aug 30 '22 at 22:13

0 Answers0