0

I have the following code in my server.js

  var cddata = [];
  body.rows.forEach(function(doc) {
  cddata.push([{id: doc.id, name: doc.key, text:doc.value.Time, group: 1}]);              
  });
  response.render('timeline', {cddata: JSON.stringify(cddata)});

and I have the following in my Jade view file

script(src='vis/dist/vis.js')        
link(rel="stylesheet", href="vis/dist/vis.css", type="text/css")

script.
    //alert(cddata);    
    var options = {};
    var data = new vis.DataSet(cddata);
    var container = document.getElementById('visualization');
    new vis.Timeline(container, data, options);

However, nothing related to the chart is rendered. I presume the object is not correctly passed to the jade file. Please help!

Also, is there a way to verify the incoming object in Jade? Alerts dont seem to work. thanks

user1384205
  • 1,231
  • 3
  • 20
  • 39

2 Answers2

0

The <script> in your jade is a browser side script so won't be able to access variables in the templates generation scope. You'll need to output your data as JSON and read it in using browser side JavaScript, something like this:

script(src='vis/dist/vis.js')        
link(rel="stylesheet", href="vis/dist/vis.css", type="text/css")

script.
    var chartData = JSON.parse('#{cddata}')
    var options = {};
    var data = new vis.DataSet(chartData);
    var container = document.getElementById('visualization');
    new vis.Timeline(container, data, options);
serby
  • 4,186
  • 2
  • 24
  • 25
  • Thanks. I changed my server file to include response.locals.docsJSON = JSON.stringify([dbdata]); and the jade file to include var chartData = JSON.parse('#{dbdata}') var options = {}; var data = new vis.DataSet(chartData); var container = document.getElementById('visualization'); new vis.Timeline(container, data, options); There is no output. I verified the JSON object sent from the server file using response.send(response.locals.docsJSON); It looks correct. – user1384205 Mar 28 '15 at 05:26
  • Also here is how the json looks when read using response.send(response.locals.docsJSON); [ [ [ { "id":"0581fe6a52fbc348f1e8825728ecea08", "name":"bdz", "text":"105", "group":1 } ], [ { "id":"2d167d859de1b4ff1d4bb3691268e3a4", "name":"bdz", "text":"30", "group":1} ], – user1384205 Mar 28 '15 at 05:35
0

After much deliberation, the following worked to pass object from node server to client side server scripting on Jade file.

on the server.js, where dbdata is an array of JSON objects

response.render('timeline', {dbdata:dbdata});

On the jade file,

script.
   var chartData = !{JSON.stringify(dbdata)};

Thanks,

elp
  • 8,021
  • 7
  • 61
  • 120
user1384205
  • 1,231
  • 3
  • 20
  • 39