【问题标题】:Express parsing JSON into template through route通过路由将 JSON 快速解析为模板
【发布时间】:2015-01-08 13:31:08
【问题描述】:

我无法通过路由解析来自 mongodb 的数据。我想返回每个对象的标题字段。

我有以下架构:

var mongoose  = require('mongoose');
var Schema    = mongoose.Schema;

var GiveSchema   = new Schema({
        title: String,
        shortname: String,
        contents: String,
        image: String,
        category: String
    });

module.exports = mongoose.model('GiveData',  GiveSchema);

我将架构存储在此变量中:

var Givedata = mongoose.model( 'GiveData' );

这是我的路线:

app.get('/', function(req, res) {
    res.render('index.ejs',{
      list: Givedata.title,
      bootstrappedUser: req.user,
      something: req.body,
      page: 'home'
    });
});

我在我的模板中使用了这个逻辑,但是想出了“未定义”

<% for(var i=0; i< list.length; i++) { %>
    <a href="/"><li><%= list[i] %></li></a>
    <% } %> 

【问题讨论】:

    标签: express ejs embedded-javascript


    【解决方案1】:

    为了让 EJS 工作,您必须将 ejs 设置为您的视图引擎:

    app.set('view engine', 'ejs');

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-12-04
      • 2014-10-14
      • 1970-01-01
      • 1970-01-01
      • 2018-08-06
      • 1970-01-01
      相关资源
      最近更新 更多