我收到错误:
barChartHelper未定义。
app.js
var express = require('express'),
app = express(),
cons = require('consolidate'),
MongoClient = require('mongodb').MongoClient,
mongodb = require('mongodb'),
Server = require('mongodb').Server;
app.engine('html', cons.swig);
app.set('view engine', 'ejs');
app.set('views', __dirname + '/views');
var mongoclient = new MongoClient(new Server("localhost", 27017));
var db = mongoclient.db('mydb');
app.use( express.static( "img" ) );
app.get('/', function(req, res){
// Find one document in our collection
db.collection('things').find({},{'Created':1, 'MRP':1}).toArray(function(err, docs){
console.log(docs);
res.render('hello',{'docs':docs});
});
});
app.get('*', function(req, res){
db.collection('things').find({},{'Created':1, 'MRP':1}).toArray(function(err, docs){
console.log(docs);
res.render('index',{'docs':docs});
});
});
mongoclient.open(function(err, mongoclient) {
if(err) throw err;
app.listen(9001);
console.log('Express server started on port 9001');
});
index.ejs
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>D3 Server-side Demo</title>
<style>
body {
background-color: #f5f5f5;
font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif;
font-size: 12px;
}
.svg-chart .background {
shape-rendering: crispEdges;
stroke: #ccc;
fill: #fdfdfd;
stroke-width: 1px;
}
.svg-chart .bar { fill: #4682B4; }
</style>
</head>
<body>
<h1>D3 Server-side Demo</h1>
<%-
barChartHelper.getBarChart({
data: docs,
width: 400,
height: 300,
xAxisLabel: '2012',
yAxisLabel: 'Views',
containerId: 'bar-chart-small'
})
%>
<hr>
<%-
barChartHelper.getBarChart({
data: docs,
width: 800,
height: 600,
xAxisLabel: '2012',
yAxisLabel: 'Views',
containerId: 'bar-chart-large'
})
%>
</body>
</html>
错误来了:
ReferenceError:/home/shubham/Music/testing/mamy/views/index.ejs:28
26 |
27 |D3服务器端演示
28 | &LT;% -
29 | barChartHelper.getBarChart({
30 |数据:docs,
31 |宽度:400,barChartHelper未在eval中定义(eval at (/home/shubham/Music/testing/mamy/node_modules/ejs/lib/ejs.js:464:12), :10:16)在returnFn (/home/shubham/Music/testing/mamy/node_modules/ejs/lib/ejs.js:493:17) 在View.exports.renderFile [作为引擎] (/home/shubham/Music/testing/mamy/node_modules/ejs/lib/ejs.js:350:31) 在View.render (/home/shubham/Music/testing/mamy/node_modules/express/lib/view.js:76:8) 在Function.app.render (/home/shubham/Music/testing/mamy/node_modules/express/lib/application.js:561:10) 在ServerResponse.res.render (/home/shubham/Music/testing/mamy/node_modules/express/lib/response.js:845:7) 在/home/shubham/Music/testing/mamy/app.js:123:11 at /home/shubham/Music/testing/mamy/node_modules/mongodb/lib/mongodb/cursor.js:173:9 在 /home/shubham/Music/testing/mamy/node_modules/mongodb/lib/mongodb/cursor.js:205:48 在 /home/shubham/Music/testing/mamy/node_modules/mongodb/lib/mongodb/cursor.js:855:28
我将相关文件 bar_chart_helper.js,bar_charts.js 放入views文件夹
答案 0 :(得分:1)
您在模板中缺少引用这些脚本的必需明确<script></script>
标记。非模板脚本只需将它们放在同一目录中就不会自动插入到模板中。