在Node js + Express + Passport + MongoDB中更新用户记录

3

好的,我已经努力了几个小时,但仍然无法弄清楚如何使它工作。

我完成了这份教程,使用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('/');

}

1个回答

2
你需要在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进行查询。

谢谢,这修复了错误 - 但是我的数据库仍然没有更新显示名称的新值。更新函数是否调用了对mongodb的更新,还是只是我刚创建的用户实例? - user1871200
此外,这是我在/app/models/user中的模式:var userSchema = mongoose.Schema({local : { email : String, password : String, displayName : String }, google : { id : String, token : String, email : String, name : String, displayName : String }}); - user1871200
谢谢,问题已经修复!我还犯了一个错误——我基于 req.user 进行渲染,而 req.user 仍然是来自 POST 请求的未更改记录。 - user1871200
@user1871200,我很高兴你的问题得到解决。 哦,是的,你说得对! - Pourya8366

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接