报错信息如下: 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| <ul class=“nav navbar-nav”> 31| <li class=“active”><a href="/">首页</a></li>
32| <% if(!user){ %> 33| <li><a href="/login">登入</a></li> 34| <li><a href="/reg">注册</a></li> 35| <% }else{ %>
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: <nav class=“navbar navbar-inverse navbar-fixed-top”> <div class=“container”> <div class=“navbar-header”> <button type=“button” class=“navbar-toggle collapsed” data-toggle=“collapse” data-target="#navbar" aria-expanded=“false” aria-controls=“navbar”> <span class=“sr-only”>Toggle navigation</span> <span class=“icon-bar”></span> <span class=“icon-bar”></span> <span class=“icon-bar”></span> </button> <a class=“navbar-brand” href="#">Microblog</a> </div> <div id=“navbar” class=“navbar-collapse collapse”> <ul class=“nav navbar-nav”> <li class=“active”><a href="/">首页</a></li> <% if(!user){ %> <li><a href="/login">登入</a></li> <li><a href="/reg">注册</a></li> <% }else{ %> <li><a href="/logout">登出</a></li> <%}%> </ul> </div><!–/.nav-collapse --> </div> </nav>
提示很明显,user 未被定义,就是说user这个变量不存在。顺着这个线索网上找,找到为什么不存在。
@Hanggi 新手求教,目前还不知道这个user从哪里来的
https://cnodejs.org/topic/540833010256839f71e05fdc you need this
@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。 不过我还是不知道该怎么改。
@eterlight 直接照着github那个做吧。。。。干嘛要学旧的。。。
@yuu2lee4 开发指南我也有一本 看看前面的就行了 那个blog的栗子太老了
@yuu2lee4 github的例子在哪里? 这本指南书我看有个完整的web例子我就照着做了,没想到到处都是问题。。。