从expres传递数组到jade客户端的javascript

我有这个玉模板:

html head script(type="text/javascript" src="https://www.google.com/jsapi") script(type='text/javascript') google.load("visualization", "1", {packages:["corechart"]}); google.setOnLoadCallback(drawChart); function drawChart() { var data = google.visualization.arrayToDataTable([ ['Date', 'Tin_A' ], - each datapoint in myData "[" + datapoint.date + "," + datapoint.value + "]," ]); var options = { title: 'bla' }; var chart = new google.visualization.LineChart(document.getElementById('chart_line')); chart.draw(data, options); } body h1= title #chart_line 

我正在使用这个调用来呈现express / node.js中的jade模板:

 app.get('/', function(req, res){ sensors.findSensorAllData(2, 2, function (error,emps){ console.log(emps); res.render('tmp', { title : 'Temperatures in a row', myData : emps }); }); }); 

console.log(emps)的输出是:

 [ { _id: 524b8642028e167fb0e3661d, sensor_id: 2, value: 49, date: Tue Oct 01 2013 20:34:40 GMT-0600 (CST) }, { _id: 524b863d028e167fb0e3661c, sensor_id: 2, value: 19, date: Tue Oct 01 2013 20:34:35 GMT-0600 (CST) } ] 

在渲染后,我期望在jade模板里有javascrip的值…但是不行。 我在纯文本中只能得到相同的行,就好像行- each datapoing in myData都没有意义…我做错了什么? 谢谢

—编辑:一切工作正常,如果我更换线

 - each datapoint in myData "[" + datapoint.date + "," + datapoint.value + "]," 

  ['2004', 20], ['2005', 30], ['2006', 40] 

       

网上收集的解决方案 "从expres传递数组到jade客户端的javascript"

我想你可能会意外地注入一个string,而不是一个数组,因为括号内的引号:

 - each datapoint in myData "[" + datapoint.date + "," + datapoint.value + "]," 

我对翡翠不是很熟悉,但是我想你可能想要做下面的事情:

 - each datapoint in myData [#{datapoint.date}, #{datapoint.value}], 

此外,在您给出的示例数据的作品中,您只使用Date的年份部分,但datapoint.date属性的内容可能是完整的Date对象,我不确定这是否是您想要的这个用法。

看到这个问题的select答案为什么你试图做不起作用。 ( JADE + EXPRESS:在内联JS代码(客户端)迭代对象? )

基本上,只要你点击脚本标记,你就会告诉Jadeparsing器以原始forms处理事情,不再进行进一步的处理。 你真正想要做的是重新为您的代码脚本标记如下所示:

 - if (typeof(pins) != "object") != "<script type='text/javascript'>" != "google.load('visualization', '1', {packages:['corechart']}); != "google.setOnLoadCallback(drawChart); != "function drawChart() { != "var data = google.visualization.arrayToDataTable([ != "['Date', 'Tin_A' ]," - forEach datapoint in myData != "[" + datapoint.date + "," + datapoint.value + "]," != "]);" != "var options = {" != "title: 'bla'" != "};" != "var chart = new google.visualization.LineChart(document.getElementById('chart_line'));" != "chart.draw(data, options);" != "}" 

试试这个,但我相当肯定它应该工作。

PS:上面的链接也(我相信)明确说明为什么以前的答案应该是不正确的,因为你不能在Jade脚本标记里面有那种模板占位符插值。

丑陋的警报:

 script ... var data = google.visualization.arrayToDataTable(['Date', 'Tin_A' ].concat(!{JSON.stringify(myData.map(function(i) { return [ i.date, i.value ] })) }));