我设置了 Mongo,我所做的只是在 index.jade 中,循环浏览一份餐食列表。
--app.js--
/** * 模块依赖。*/
var express = require('express')
mongoose = require('mongoose')
, routes = require('./routes')
, meal = require('./routes/meal')
, http = require('http')
, path = require('path');
var app = express();
// all environments
app.set('port', process.env.PORT || 3000);
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
app.use(require('less-middleware')({ src: __dirname + '/public' }));
app.use(express.static(path.join(__dirname, 'public')));
// development only
if ('development' == app.get('env')) {
app.use(express.errorHandler());
}
app.get('/', routes.index);
app.get('/new', meal.new);
app.get('/meals', meal.getMeals)
app.post('/meals', meal.add)
app.get('')
http.createServer(app).listen(app.get('port'), function(){
console.log('Express server listening on port ' + app.get('port'));
});
--meals.js--
var mongoose = require('mongoose');
mongoose.connect("mongodb://localhost/nutritionTracker")
var MealSchema = new mongoose.Schema({
mealId: Number,
mealType: String,
mealDetails: String
}),
Meals = mongoose.model('Meals', MealSchema);
/*
* New meal.
*/
exports.new = function(req, res){
res.render('new');
};
/*
* Insert new meal.
*/
exports.add = function(req, res){
var meal = req.body;
new Meals({
mealId: 1,
mealType: meal.mealType,
mealDetails: meal.mealDetails
}).save(function (err, docs) {
if (err) res.render("Error")
});
res.render('new');
};
/*
* Get all meals.
*/
exports.getMeals = function(req, res){
Meals.find({}, function(err, docs) {
res.render('index', { meals: docs });
});
};
--index.jade--
扩展布局
block content
table.table.table-striped
thead
tr
th Meal Type
th Meal Details
tbody
each meal in meals
tr
td #{meal.type}
td #{meal.details}
a(href="/new", class="btn btn-primary btn-lg") Add Meal
我不断收到此错误:
类型错误:...\views\index.jade:10 8| 餐点详情 9| tbody > 10| 每餐中餐 11| 12| td #{meal.type} 13| td #{meal.details} 无法读取未定义的属性“长度”
有任何想法吗?