简体   繁体   English

未捕获到的SyntaxError:意外令牌var Node.js

[英]Uncaught SyntaxError: Unexpected token var Node.js

I'm working with Node.js and trying to display a chart, generated out of coordinates from a txt file, which is uploaded to the server. 我正在使用Node.js,并试图显示图表,该图表是根据txt文件的坐标生成的,并已上传到服务器。 My problem: When I open the web page and upload the file, everything works perfectly, but the chart is not working: I get the following error: UncaughtSyntaxError: Unexpected token var. 我的问题:打开网页并上传文件时,一切正常,但是图表却无法正常工作:我收到以下错误: UncaughtSyntaxError:意外的令牌var。

If I render the webpage without node.js everything works fine. 如果我在不使用node.js的情况下呈现网页,则一切正常。

The code: 编码:

var express = require("express"); 
var app = express();
var fs = require('fs');  // we will need it for file uploads
var port = process.env.PORT || 3000;

app.configure(function(){
  app.use(express.bodyParser());
  app.use(express.methodOverride());
  app.use(app.router);
  app.use(express.static(__dirname + '/public'));
});

app.get('/', function(req, res) {

     res.send('<form method="post" enctype="multipart/form-data">'
    + '<p>Image: <input type="file" name="uploadfile" /></p>'
    + '<p><input type="submit" value="Upload" /></p>'
    + '</form>');

});

app.post('/', function(req, res){

    // .. Upload and file reading 
    // .. var obj[key1] (X-Values) and var obj[key2] (Y-Values) are generated 
    // .. 
                    res.send('<head><title>Line Chart</title><script src="../Chart.js"></script><meta name = "viewport" content = "initial-scale = 1, user-scalable = no"><style>canvas{    }</style></head><body><canvas id="canvas" height="450" width="600"></canvas>'
                    +'<script type="text/javascript">'  
                    +' ' 
                    +'var lineChartData = { ' 
                    +'labels : [ ' + obj[key1] + ' ],' 
                    +'datasets : [{' 
                    +'fillColor : "rgba(151,187,205,0.5)",' 
                    +'strokeColor : "rgba(0, 0, 0, 0.831373)",' 
                    +'pointColor : "rgba(151,187,205,1)",' 
                    +'pointStrokeColor : "gba(76, 255, 178, 0.831373)",' 
                    +'data : ['+ obj[key2] + ' ]' 
                    +'} ] } '  
                    +'var myLine = new Chart(document.getElementById("canvas").getContext("2d")).Line(lineChartData);' 
                    +'</script>'
                    +'</body></html>'
                    );
            };
        });

     });        
});

app.listen(port, function() {
  console.log("Listening on " + port);
});

I hope you can help me.. 我希望你能帮帮我..

Greetings, 问候,

JS JS

Since you don't use newlines in your code sample this will be the output: 由于您不在代码示例中使用换行符,因此将输出:

var lineChartData = { labels : [ undefined ],datasets : [{fillColor : "rgba(151,187,205,0.5)",strokeColor : "rgba(0, 0, 0, 0.831373)",pointColor : "rgba(151,187,205,1)",pointStrokeColor : "gba(76, 255, 178, 0.831373)",data : [undefined ]} ] } var myLine = new Chart(document.getElementById("canvas").getContext("2d")).Line(lineChartData);

Which is invalid Javascript since you need to close the 'lineChardData' object with a ; 这是无效的Javascript,因为您需要使用;来关闭'lineChardData'对象。

Change it to this and it will work: 将其更改为此,它将起作用:

res.send('<head><title>Line Chart</title><script src="../Chart.js"></script><meta name = "viewport" content = "initial-scale = 1, user-scalable = no"><style>canvas{    }</style></head><body><canvas id="canvas" height="450" width="600"></canvas>'
                    +'<script type="text/javascript">'  
                    +' ' 
                    +'var lineChartData = { ' 
                    +'labels : [ ' + obj[key1] + ' ],' 
                    +'datasets : [{' 
                    +'fillColor : "rgba(151,187,205,0.5)",' 
                    +'strokeColor : "rgba(0, 0, 0, 0.831373)",' 
                    +'pointColor : "rgba(151,187,205,1)",' 
                    +'pointStrokeColor : "gba(76, 255, 178, 0.831373)",' 
                    +'data : ['+ obj[key2] + ' ]' 
                    +'} ] }; '  
                    +'var myLine = new Chart(document.getElementById("canvas").getContext("2d")).Line(lineChartData);' 
                    +'</script>'
                    +'</body></html>'
                    );

Also I don't think it's nice or good practise to output HTML/javascript data directly from Node.js this way .. 同样,我也不认为以这种方式直接从Node.js输出HTML / javascript数据是一个好方法。

You're not sending any line breaks so (trimmed down) the JavaScript interpreter sees the following invalid code: 您没有发送任何换行符,因此(已精简)JavaScript解释器会看到以下无效代码:

var lineChartData = { /*...*/} var myLine = new Chart(/*...*/);

Put a semicolon after the first var statement and it will work: 在第一个var语句之后放一个分号,它将起作用:

var lineChartData = { /*...*/}; var myLine = new Chart(/*...*/);
//                            ^ Put me here      

If there was a line break in the output it would work without the semicolon as ASI would step in, so that's another possible solution. 如果输出中出现换行符,则可以像ASI那样使用分号来工作,因此这是另一种可能的解决方案。

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

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