好的,所以我几个小时都在苦苦挣扎,无法弄清楚如何让它发挥作用。
我按照本教程完成了使用mongodb,express和passport设置nodejs: https://scotch.io/tutorials/easy-node-authentication-setup-and-local 它工作得很好,除了现在我想使用经过身份验证的会话允许用户通过表单创建显示名称。
我很难过,因为我无法弄清楚如何使用路由/护照/会话方法通过POST请求更新mongodb中的用户记录。
我的问题与下面的“流程更新配置文件”代码块有关。我正在尝试使用user.update函数将displayname添加到我的用户记录中。我收到以下错误:
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('/');
}
答案 0 :(得分:3)
您应该在routes.js文件的顶部添加此行:
var user = require('../app/models/user'); //i get the address of user model in the link you give, but in general it should be the user model address.
因为当你使用更新功能时,下面代码中的user
应该是你的MongoDB模型的名称。
user.update({_id: req.session.passport.user}, {
displayName: req.body.displayName
},function(err, numberAffected, rawResponse) {
console.log('new profile update error');
});
正确的更新查询是:
{_id: req.session.passport.user.id}
如果你想根据它的id找到一个用户,那么你应该用用户的id进行查询。