好的,我已经努力了几个小时,但仍然无法弄清楚如何使它工作。
我完成了这份教程,使用nodejs、mongodb、express和passport进行设置: https://scotch.io/tutorials/easy-node-authentication-setup-and-local 它运行得很好,但现在我想使用已验证的会话通过表单允许用户创建一个显示名称。
我被难住了,因为我不知道如何使用路由/ passport / session方法通过POST请求更新mongodb中的用户记录。
我的问题与下面的“Process Update Profile”代码块有关。 我正在尝试使用user.update函数向我的用户记录添加显示名称。 我收到以下错误:
ReferenceError: user is not defined
at Object.handle (/vagrant/new/app/routes.js:65:9)
at next_layer (/vagrant/new/node_modules/express/lib/router/route.js:103:13)
at Object.isLoggedIn [as handle] (/vagrant/new/app/routes.js:115:16)
at next_layer (/vagrant/new/node_modules/express/lib/router/route.js:103:13)
at Route.dispatch (/vagrant/new/node_modules/express/lib/router/route.js:107:5)
at /vagrant/new/node_modules/express/lib/router/index.js:195:24
at Function.proto.process_params (/vagrant/new/node_modules/express/lib/router/index.js:251:12)
at next (/vagrant/new/node_modules/express/lib/router/index.js:189:19)
at next_layer (/vagrant/new/node_modules/express/lib/router/route.js:77:14)
at next_layer (/vagrant/new/node_modules/express/lib/router/route.js:81:14)
at next_layer (/vagrant/new/node_modules/express/lib/router/route.js:81:14)
at Route.dispatch (/vagrant/new/node_modules/express/lib/router/route.js:107:5)
at /vagrant/new/node_modules/express/lib/router/index.js:195:24
at Function.proto.process_params (/vagrant/new/node_modules/express/lib/router/index.js:251:12)
at next (/vagrant/new/node_modules/express/lib/router/index.js:189:19)
at next (/vagrant/new/node_modules/express/lib/router/index.js:166:38)
我发现该函数没有传递用户参数,但我无法确定正确的方法是什么,或者我是否完全正确地处理了这个问题。 请看下面的代码:
// app/routes.js
module.exports = function(app, passport) {
// =====================================
// HOME PAGE (with login links) ========
// =====================================
app.get('/', function(req, res) {
res.render('index.ejs'); // load the index.ejs file
});
//... some code (login, signup methods) omitted for brevity ...
// =====================================
// UPDATE PROFILE =================
// =====================================
app.get('/updateprofile', isLoggedIn, function(req, res) {
res.render('updateprofile.ejs', {
user : req.user // get the user out of session and pass to template
});
});
// =====================================
// PROCESS UPDATE PROFILE=======================
// =====================================
// process the update profile form
app.post('/updateprofile', isLoggedIn, function(req, res) {
user.update({_id: req.session.passport.user}, {
displayName: req.body.displayName
}, function(err, numberAffected, rawResponse) {
console.log('new profile update error');
});
res.render('profile.ejs', {
user : req.user // get the user out of session and pass to template
});
});
// =====================================
// PROFILE SECTION =====================
// =====================================
// we will want this protected so you have to be logged in to visit
// we will use route middleware to verify this (the isLoggedIn function)
app.get('/profile', isLoggedIn, function(req, res) {
res.render('profile.ejs', {
user : req.user // get the user out of session and pass to template
});
});
}; function isLoggedIn(req, res, next) {
// if user is authenticated in the session, carry on
if (req.isAuthenticated())
return next();
// if they aren't redirect them to the home page
res.redirect('/');
}
local : { email : String, password : String, displayName : String }, google : { id : String, token : String, email : String, name : String, displayName : String }
}); - user1871200