简体   繁体   English

从快递到jade客户端javascript传递数组

[英]passing an array from express to jade client side javascript

I have this jade template: 我有这个玉模板:

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

and I'm using this call to render the jade template in express/node.js: 我正在使用此调用在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
        });
    });
});

the output of the console.log(emps) is: 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) } ]

after the rendering occour, I expect to have the values within the javascrip in the jade template... but It won't work. 在渲染出现之后,我希望在jadecrip中有jadecrip中的值......但是它不起作用。 I get only the same lines in plain text, as if the line - each datapoing in myData would have no meaning... what am I doing wrong? 我在纯文本中只得到相同的行,就好像行- each datapoing in myData都没有意义......我做错了什么? Thanks 谢谢

--- Edit: Everything works fine if I replace the lines ---编辑:如果我更换线条,一切正常

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

with

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

I think you may be accidentally injecting a String instead of an Array because of the quotes around the brackets: 我认为由于括号周围的引号,您可能会意外地注入String而不是Array:

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

I'm not very familiar with Jade, but I think you may want to do the following instead: 我对Jade不太熟悉,但我想你可能想要做以下事情:

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

Also, in the sample data you gave that works, you are only using the year portion of the Date, but the contents of the datapoint.date property may be a full Date object, I'm not sure if that is what you want for this use. 此外,在您提供的示例数据中,您只使用Date的year部分,但datapoint.date属性的内容可能是完整的Date对象,我不确定这是否是您想要的这个用途。

See this question's chosen answer for why what you're trying to do doesn't work. 看到这个问题的选择答案为什么你要做的事情不起作用。 ( JADE + EXPRESS: Iterating over object in inline JS code (client-side)? ) JADE + EXPRESS:在内联JS代码(客户端)中迭代对象?

Basically, as soon as you hit the script tag, you're telling the Jade parser to handle things in raw form, and no further processing is done. 基本上,只要你点击脚本标签,你就告诉Jade解析器以原始形式处理事物,而不进行进一步的处理。 What you really want to do is redo the script tag for your code like follows: 您真正想要做的是重做代码的脚本标记,如下所示:

- 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);"
!=   "}"

Try this, but I'm fairly certain it should work. 试试这个,但我相当肯定它应该有效。

PS: The link above also (I believe) clearly states why the previous answer should be incorrect, as you can't have that kind of template placeholder interpolation inside of Jade script tags. PS:上面的链接(我相信)清楚地说明为什么前面的答案应该是不正确的,因为你不能在Jade脚本标签内部进行那种模板占位符插值。

Ugly alert: 丑陋警报:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM