0

我想为每个登录的用户创建一个不同的基本用户配置文件。寻找建议/帮助以找出为什么这不起作用。我正在使用 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'});
};

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

谢谢!

4

0 回答 0