尝试使用jade布局创建自定义用户配置文件时出错?

时间:2013-06-02 08:13:47

标签: node.js mongodb session-cookies pug

我希望为每个用户登录后创建一个不同的基本用户配置文件。 寻找建议/帮助,找出为什么这不起作用。我正在使用nodejs和mongodb。目前我在用户登录后存储会话(我认为我是这样):

var Session = require('connect-mongo')(connect);

app.use(express.session({ 
    secret: 'settings.cookie_secret',
    store: new Session({
        url: 'mongodb://localhost/test5',
        maxAge: 300000
    })
}));

如果此步骤错误,请解释

然后我在这里得到了我的玉文件:

extends layout

block content
div.centerContent
    - if (locals.user)
    p Welcome #{user.email}
    - else 
    p Welcome

我的路线档案

var User = require('../models/user.js');
var passport = require('passport');

exports.register = function(req, res) {
res.render('register', {title: 'Weblio'});
};

exports.registerThanks = function(req, res) {
res.render('registerThanks', {title: 'Weblio'});
};
exports.registerError = function(req, res) {
res.render('registerError', {title: 'Weblio'});
};

exports.registerPost = function(req, res, next) {
console.log("here 1");
if(req.body && req.body.email && req.body.password === req.body.cpassword) {
    var newUser = new User ({
        firstName: req.body.firstName,
        lastName: req.body.lastName,
        email: req.body.email,
        password: req.body.password,
        phone: req.body.phone,
        birthday: new Date(req.body.birthday) 
    });
    newUser.save(function(err){
        console.log("here 3");
        if(!err) {
            console.log("here3a");
            res.redirect('registerThanks'); 
        } else {
            {email: {$exists: true}};
            console.log("here 4");
            res.redirect('registerError');
    /*} else {

            console.log(email);
            console.log('email exists');
            res.redirect('registerError');
            next(err);
        }; */
        //removed next(err); from above else and now it does not show dup key error
    }
});
} else {
    console.log("here 1a");
    next(new Error('Incorrect POST'));
    res.redirect('registerError');
}
};

exports.login = function(req, res) {
res.render('login', {user: req.user, title: 'Weblio'});
};

exports.loginPost = function(req, res, next) {
console.log("here 1");
passport.authenticate('local', function(err, user, info) {
    console.log("here 2");
    if (err) { return next(err) }
    if (!user) { return res.redirect('loginError'); }
    req.logIn(user, function(err) {
      if (err) { return next(err); }
  return res.redirect('userProfile');
    });
  })(req, res, next);
};

exports.loginError = function (req, res) {
res.render('loginError', {title: 'Weblio'});
};

exports.userProfile = function(req, res) {
res.render('userProfile', {title: 'Weblio'});
};

它给我一个未定义对象的错误 - 如何从db访问对象,只要用户登录就让他们看到这个页面。

谢谢!

0 个答案:

没有答案