【问题标题】:Render mongo data to ejs template将 mongo 数据渲染到 ejs 模板
【发布时间】:2016-03-01 05:39:55
【问题描述】:

nodejs 新手。一直在努力解决这个基本问题。我在 mongo 数据库中有一些数据。我正在使用带有 ejs 视图引擎的 express。我可以看到来自 console.log 的 mongo 数据输出。而且我知道我可以使用以下标签将我的数据插入到 ejs 模板中:,但我似乎无法弄清楚如何在 res.render 代码中呈现它。

这是从 mongo 中提取数据的代码:

MongoClient.connect(url, function (err, db) {
  if (err) {
    console.log('Unable to connect to the mongoDB server. Error:', err);
  } else {
    console.log('Connection established to', url);


    // Get the documents collection
    var collection = db.collection('car');

    // Query the collection    
     collection.find(),(function (err, result) {
      if (err) {
        console.log(err);
      } else if (result.length) {
        console.log('Found:', result);
      } else {
        console.log('No document(s) found with defined "find" criteria!');
      }
//Close connection
      db.close();
    });
  }
});

这是渲染部分:

// index page
app.get('/', function(req, res) {
  res.render('pages/index', {collection: result} );
});

这是 ejs 模板:

<h2>Cars:</h2>
        <h3><%= result %>

这是我在浏览器中看到的错误:

ReferenceError: 结果未定义

这非常令人沮丧,因为这应该是一个简单的直接...我正在尝试不使用猫鼬来做到这一点。

任何帮助将不胜感激。

谢谢。

【问题讨论】:

    标签: node.js mongodb ejs


    【解决方案1】:

    试试

    <%= collection %>
    

    您必须使用 KEY 而不是来自 json 对象的 VALUE。

    希望您在 express 模块中正确设置了视图目录和视图引擎。

    【讨论】:

      【解决方案2】:

      你的代码有两个问题:

      1) 你需要有一个可以传递给视图的结果对象:

      function getResult(callback) {
          MongoClient.connect(url, function (err, db) {
            if (err) {
              console.log('Unable to connect to the mongoDB server. Error:', err);
            } else {
              console.log('Connection established to', url);
      
      
              // Get the documents collection
              var collection = db.collection('car');
      
              // Query the collection    
              collection.find(),(function (err, result) {
                if (err) {
                  console.log(err);
                } else if (result.length) {
                  console.log('Found:', result);
                } else {
                  console.log('No document(s) found with defined "find" criteria!');
                }
                //Close connection
                db.close();
                callback(err, result);
              });
            }
         });   
      }
      // index page
      app.get('/', function(req, res) {
        var result = getResult(function(err, result){
            //handle err, then you can render your view
            res.render('pages/index', {collection: result} );
        });
      
      });
      

      2) 在您的视图中,您将拥有一个名为集合的对象(不是结果):

      <h2>Cars:</h2>
          <h3><%= collection %>
      

      【讨论】:

      • 谢谢。我已在 index.ejs 中将其更改为 但仍然收到 ReferenceError: result is not defined...
      • 好的,我错过了您为回调添加的代码。我已经修改了我的 app.js 中的代码。我已连接到数据库(如 console.log 输出所示),但浏览器挂在那里 localhost:8080 然后没有连接...
      • 这是集成了 getResult 回调的新代码:
      猜你喜欢
      • 2016-03-06
      • 1970-01-01
      • 2012-05-08
      • 2018-08-02
      • 2016-02-26
      • 2018-02-09
      • 2015-07-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多