<!DOCTYPE html>
<html>
<head>
<title><%= title %></title>
</head>
<body>
<h1><%= lang %></h1>
<% if(authenticated){ %>
<p>Welcome back, <%= me.first%></p>
<a href="/logout">Logout</a>
<% }else{ %>
<ul>
<li><a href="/login">Login</a></li>
<li><a href="/signup">Signup</a></li>
</ul>
<% } %>
</html>
app.use(function(req,res,next){
if(req.session.loggedIn){
res.locals.authenticated = true;
MongoClient.connect(url, function(err, db){
console.log("Connected correctly to server");
var collection = db.collection('users');
collection.findOne({id:{$oid:req.session.loggedIn}},function(err,doc){
if(err) throw err;
res.locals.me = doc;
next();
})
});
}else{
res.locals.authenticated = false;
next();
}
})
最后运行的结果是authenticated是undefined,不知道出现这种错误的原因是不是我的语法有问题?请前辈们解惑,谢谢。