我正在尝试使用教程创建节点博客,但我一直在布局中遇到错误。我完全复制并粘贴了教程,但仍然没有运气。
layout.jade
extends layout
block content
h1= title
#articles
-each article, i in articles
div.article
div.created_at= article.created_at
div.title
a(href="/blog/"+article._id)!= article.title
div.body= article.body
错误
C:\ Users \ pncha_000 \ Documents \ Code \ SimpleBlog \ views \ index.jade:6 4 | h1 =标题5 | #articles> 6 | - 每篇文章,我在文章7 | div.article 8 | div.created_at = article.created_at 9 | div.title无法读取未定义的属性“长度”
TypeError: C:\Users\pncha_000\Documents\Code\SimpleBlog\views\index.jade:6
4| h1= title
5| #articles
> 6| -each article, i in articles
7| div.article
8| div.created_at= article.created_at
9| div.title
Cannot read property 'length' of undefined
at eval (eval at <anonymous> (C:\Users\pncha_000\Documents\Code\SimpleBlog\node_modules\jade\lib\jade.js:172:8), <anonymous>:47:31)
at eval (eval at <anonymous> (C:\Users\pncha_000\Documents\Code\SimpleBlog\node_modules\jade\lib\jade.js:172:8), <anonymous>:126:4)
at eval (eval at <anonymous> (C:\Users\pncha_000\Documents\Code\SimpleBlog\node_modules\jade\lib\jade.js:172:8), <anonymous>:139:21)
at res (C:\Users\pncha_000\Documents\Code\SimpleBlog\node_modules\jade\lib\jade.js:173:38)
at Object.exports.render (C:\Users\pncha_000\Documents\Code\SimpleBlog\node_modules\jade\lib\jade.js:269:10)
at Object.exports.renderFile (C:\Users\pncha_000\Documents\Code\SimpleBlog\node_modules\jade\lib\jade.js:305:18)
at View.exports.renderFile [as engine] (C:\Users\pncha_000\Documents\Code\SimpleBlog\node_modules\jade\lib\jade.js:290:21)
at View.render (C:\Users\pncha_000\Documents\Code\SimpleBlog\node_modules\express\lib\view.js:76:8)
at Function.app.render (C:\Users\pncha_000\Documents\Code\SimpleBlog\node_modules\express\lib\application.js:503:10)
at ServerResponse.res.render (C:\Users\pncha_000\Documents\Code\SimpleBlog\node_modules\express\lib\response.js:802:7)
app.js
var express = require('express');
var path = require('path');
var favicon = require('static-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var routes = require('./routes/index');
var users = require('./routes/users');
var app = express();
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
app.use(favicon());
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded());
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use('/', routes);
app.use('/users', users);
/// catch 404 and forward to error handler
app.use(function(req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});
/// error handlers
// development error handler
// will print stacktrace
if (app.get('env') === 'development') {
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: err
});
});
}
// production error handler
// no stacktraces leaked to user
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: {}
});
});
app.get('/', function(req, res){
articleProvider.findAll(function(error, docs){
res.send('index.jade', { locals: {
title: 'Blog',
articles:docs
}
});
});
});
module.exports = app;
articleprovider-memory.js
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
var articleCounter = 1;
ArticleProvider = function(){};
ArticleProvider.prototype.dummyData = [];
ArticleProvider.prototype.findAll = function(callback) {
callback( null, this.dummyData )
};
ArticleProvider.prototype.findById = function(id, callback) {
var result = null;
for(var i =0;i<this.dummyData.length;i++) {
if( this.dummyData[i]._id == id ) {
result = this.dummyData[i];
break;
}
}
callback(null, result);
};
ArticleProvider.prototype.save = function(articles, callback) {
var article = null;
if( typeof(articles.length)=="undefined")
articles = [articles];
for( var i =0;i< articles.length;i++ ) {
article = articles[i];
article._id = articleCounter++;
article.created_at = new Date();
if( article.comments === undefined )
article.comments = [];
for(var j =0;j< article.comments.length; j++) {
article.comments[j].created_at = new Date();
}
this.dummyData[this.dummyData.length]= article;
}
callback(null, articles);
};
/* Lets bootstrap with dummy data */
new ArticleProvider().save([
{title: 'Post one', body: 'Body one', comments:[{author:'Bob', comment:'I love it'}, {author:'Dave', comment:'This is rubbish!'}]},
{title: 'Post two', body: 'Body two'},
{title: 'Post three', body: 'Body three'}
], function(error, articles){});
exports.ArticleProvider = ArticleProvider;