嘿,所以当我尝试访问一个不登录就无法访问的页面时,应用程序崩溃了。我该如何避免这种情况?它崩溃的原因是当他们尝试访问该页面时,该页面会尝试加载未定义的数据。
错误是
TypeError:无法读取 null 的属性“firstName”。
这在我尝试访问 UserProfile 页面后加载。
我不确定在哪里放置一个人不能访问除主页、登录、注册页面之外的任何其他页面,除非他们已经登录?
app.js 文件:
var express = require('express')
, app = express()
, dbUserModel = require('./models/user')
, db = require('./db')
, pass = require('./config/passport')
, passport = require('passport')
, routes = require('./routes/index')
, user = require('./routes/user')
, path = require('path')
, http = require('http')
, connect = require('connect')
, mongoose = require('mongoose')
, mongoConnect = mongoose.connect('mongodb://localhost/test5');
// all environments
app.configure(function(){
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.cookieParser('sabkdasdkjhakhfkahf7232ujdijaw9jok&^&^@88'));
//app.use(express.cookieSession());
app.use(express.methodOverride());
app.use(passport.initialize());
app.use(passport.session());
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));
});
app.get('/', routes.index);
app.get('/register', user.register);
app.post('/register', user.registerPost);
app.get('/login', user.login);
app.post('/login', user.loginPost);
app.get('/userProfile', user.userProfile);
//app.get('/contacts', user.contacts);
app.get('/editUserProfile', user.editUserProfile);
app.post('/editUserProfile', user.editUserProfilePost);
http.createServer(app).listen(app.get('port'), function(){
console.log('Express server listening on port ' + app.get('port'));
console.log('Users: ' + db.collections.users);
});