node.js - Scope of exported variable in express and jade -


i'm having few issues jade , scope of variables exported route. might obvious answer, googling abilities have failed me.

in route, have code :

res.render('index', {title: "app",                      csvdata: json  // json object }; 

in view, want display length of json object on click of button. jade looks :

extends layout block content   script     -var test123 = csvdata;     -console.log(test123.length);   div      button.btncsv(onclick='console.log(test123)') save csv 

the first console.log prints correct length, when press button, tells me test123 undefined. think has difference between client side/server side variables. if case, there anyway make server side variable accessible client side scope?

i'm not sure how example work script content prefixed -, indicates unbuffered code. javascript runs server side , produces no direct output, in-line script empty.

similarly onclick handler compiling string on server, main problem appear report.

in order achieve trying do, should define function in script block can called buttons onclick handler. take care ensure script keyword ends . following lines treated block content of script.

here's template should like.

extends layout block content   script.     var test123 = !{json.stringify(csvdata)};     function printlength() {        console.log(test123.length);     }   div      button.btncsv(onclick='printlength()') save csv 

then on server side make sure you're returning actual javascript object, or array, , not string representation... should this

res.render('index', {title: "app",                      csvdata: [{ val1: 'value1', val2: 'value2' }]   }; 

this allows variables used server side scripting (if required) client side.


Comments

Popular posts from this blog

python - Subclassed QStyledItemDelegate ignores Stylesheet -

java - HttpClient 3.1 Connection pooling vs HttpClient 4.3.2 -

SQL: Divide the sum of values in one table with the count of rows in another -