【问题标题】:ReferenceError : barChartHelper is not defined?ReferenceError : barChartHelper 没有定义?
【发布时间】: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


    【解决方案1】:

    您在引用这些脚本的模板中缺少必需的显式 &lt;script&gt;&lt;/script&gt; 标记。非模板脚本不会自动插入到您的模板中,只需将它们放在同一目录中即可。

    【讨论】:

    • 如果我添加一个 而不是 那么它没有显示任何东西
    • 不,您必须在您的模板 添加脚本标签(例如在&lt;head&gt;&lt;/head&gt;内)。喜欢&lt;script src="/js/bar_charts.js"&gt;&lt;/script&gt;&lt;script src="/js/bar_chart_helper.js"&gt;&lt;/script&gt;。此外,您必须将这两个文件移动到您的公共目录中,以便浏览器可以访问它们。
    • 哦,谢谢,我需要在服务器端添加这一行 app.locals.barChartHelper = require('./bar_chart_helper');
    猜你喜欢
    • 2019-08-07
    • 2020-07-13
    • 1970-01-01
    • 1970-01-01
    • 2021-03-13
    • 2021-01-16
    • 2018-11-06
    • 1970-01-01
    • 2015-07-19
    相关资源
    最近更新 更多