【发布时间】:2016-01-02 03:15:17
【问题描述】:
我收到错误:
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>
错误来了:
参考错误:/home/shubham/Music/testing/mamy/views/index.ejs:28
26|
27|D3 服务器端演示
28| 29| barChartHelper.getBarChart({
30|数据:文档,
31|宽度:400,barChartHelper 未在 eval 处定义(eval at (/home/shubham/Music/testing/mamy/node_modules/ejs/lib/ejs.js:464:12), :10:16) 在返回Fn (/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 在 /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 到意见文件夹
【问题讨论】:
标签: javascript node.js mongodb d3.js