好的,所以我一直在为此苦苦挣扎几个小时,无法弄清楚如何让它工作。在Node js + Express + Passport + MongoDB中更新用户记录
我也跟着上设置的NodeJS本教程完成的MongoDB,Express和护照: https://scotch.io/tutorials/easy-node-authentication-setup-and-local 它的伟大工程,但现在我想用验证的会话才能允许用户通过表单创建的显示名称。
我很难过,因为我无法弄清楚如何使用route/passport/session方法通过POST请求更新mongodb中的用户记录。
我的问题涉及下面的'过程更新配置文件'代码块。我试图使用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
});
});
}; 功能isLoggedIn(REQ,水库,下一个){
// 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('/');
}
谢谢,这解决了错误 - 但是,我的数据库仍然不更新显示名称的新值。是更新函数调用更新到mongodb还是只是刚刚创建的用户实例? – user1871200
而且,这是我在/ app /模型/用户模式: VAR userSchema = mongoose.Schema({ 地方:{ 电子邮件:字符串, 密码:字符串, 显示名:字符串 }, 谷歌:{ ID:字符串, 令牌:字符串, 电子邮件:字符串, 名称:字符串, 显示名:字符串 } }); – user1871200
@ user1871200,我为你更新了答案。 – Pourya8386