报错信息如下: E:\Java\nodeJsLearn\microblog>node app.js connect.multipart() will be removed in connect 3.0 visit https://github.com/senchalabs/connect/wiki/Connect-3.0 for alternatives connect.limit() will be removed in connect 3.0 Express server listening on port 3000 ReferenceError: E:\Java\nodeJsLearn\microblog\views\layout.ejs:32 30|
-
31|
- 首页
- 登入 34|
- 注册 35| <% }else{ %>
32| <% if(!user){ %> 33|
user is not defined at eval (eval at <anonymous> (E:\Java\nodeJsLearn\microblog\node_modules\ejs \lib\ejs.js:464:12), <anonymous>:13:12) at returnedFn (E:\Java\nodeJsLearn\microblog\node_modules\ejs\lib\ejs.js:493 :17) at View.exports.renderFile [as engine] (E:\Java\nodeJsLearn\microblog\node_m odules\ejs\lib\ejs.js:350:31) at View.render (E:\Java\nodeJsLearn\microblog\node_modules\express\lib\view. js:76:8) at Function.app.render (E:\Java\nodeJsLearn\microblog\node_modules\express\l ib\application.js:504:10) at ServerResponse.res.render [as partial] (E:\Java\nodeJsLearn\microblog\nod e_modules\express\lib\response.js:798:7) at E:\Java\nodeJsLearn\microblog\node_modules\express-partials\index.js:75:1 3 at View.exports.renderFile [as engine] (E:\Java\nodeJsLearn\microblog\node_m odules\ejs\lib\ejs.js:355:10) at View.render (E:\Java\nodeJsLearn\microblog\node_modules\express\lib\view. js:76:8) at Function.app.render (E:\Java\nodeJsLearn\microblog\node_modules\express\l ib\application.js:504:10)
E:\Java\nodeJsLearn\microblog\node_modules\connect-mongodb\lib\connect-mongodb.j s:146 _collection.update({_id: sid}, update, {upsert: true}, function (err, data) ^ TypeError: Cannot call method ‘update’ of null at MongoStore.MONGOSTORE.set (E:\Java\nodeJsLearn\microblog\node_modules\con nect-mongodb\lib\connect-mongodb.js:146:15) at Session.save (E:\Java\nodeJsLearn\microblog\node_modules\express\node_mod ules\connect\node_modules\express-session\session\session.js:63:25) at ServerResponse.res.end (E:\Java\nodeJsLearn\microblog\node_modules\expres s\node_modules\connect\node_modules\express-session\index.js:304:19) at E:\Java\nodeJsLearn\microblog\node_modules\express\node_modules\connect\n ode_modules\errorhandler\index.js:68:17 at fs.js:266:14 at Object.oncomplete (fs.js:107:15)
以下是app.js部分代码和layout.ejs部分代码: var express = require(‘express’); var routes = require(’./routes’); var user = require(’./routes/user’); var http = require(‘http’); var path = require(‘path’); var MongoStore = require(‘connect-mongodb’); var settings = require(’./settings’);
var partials = require(‘express-partials’);
var app = express(); // all environments app.set(‘port’, process.env.PORT || 3000); app.set(‘views’, path.join(__dirname, ‘views’)); app.set(‘view engine’, ‘ejs’); app.use(partials()); app.use(express.favicon()); app.use(express.logger(‘dev’)); app.use(express.json()); app.use(express.urlencoded()); app.use(express.methodOverride()); //app.use(app.router); app.use(express.static(path.join(__dirname, ‘public’)));
app.configure(function(){ app.set(‘views’, path.join(__dirname, ‘views’)); app.set(‘view engine’, ‘ejs’); app.use(express.bodyParser()); app.use(express.methodOverride()); app.use(express.cookieParser()); app.use(express.session({ secret:settings.cookieSecret, store:new MongoStore({ db:settings.db }) })); //app.use(express.router(routes)); app.use(app.router); app.use(express.static(__dirname+’/public’)); }); var flash = require(‘connect-flash’); app.use(flash()); app.use(function(req,res,next){ res.locals.user = req.session.user; var err = req.flash(‘error’); if(err.length){res.locals.error = err;} else{res.locals.error = null;} var succ = req.flash(‘success’); if(succ.length){res.locals.success = succ;} else{res.locals.success = null;} next(); }); routes(app); // development only if (‘development’ == app.get(‘env’)) { app.use(express.errorHandler()); }
layout.ejs:
@yuu2lee4 我用的express是3.5.0,这个是4.x。据说差别还是很大的。user这个报错的地方我应该是找到了: 《指南》书上用的是 app.dynamicHelpers({ user:function(req,res){ return req.session.user; }, error:function(req,res){ var err = req.flash(‘error’); if(err.length){return err;} else{return null;} }, success:function(req,res){ var succ = req.flash(‘success’); if(succ.length){return succ;} else{return null;} }, }); 3.5版本已经没有这个dynamicHelpers方法了,然后按照网上的改成这样子: app.locals({ inspect:function(obj){ return util.inspect(obj,true); } }); app.use(function(req,res,next){ res.locals.headers = req.headers; next(); }); app.get(’/helper’,function(req,res){ res.render(‘helper’,{ title:‘Helpers’ }); });
我看书上的这个有user、success、error所以layout.js里面的<% if(!user){ %>才可以用,网上这种写法导致<% if(!user){ %> user undefined。 不过我还是不知道该怎么改。