【问题标题】:Updating a user record in Node js + Express + Passport + MongoDB在 Node js + Express + Passport + MongoDB 中更新用户记录
【发布时间】:2016-08-15 05:11:39
【问题描述】:

好的,所以我已经为此苦苦挣扎了好几个小时,不知道如何让它发挥作用。

我按照本教程完成了使用 mongodb、express 和 passport 设置 nodejs: 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, 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('/');

}

【问题讨论】:

    标签: node.js mongodb express passport.js


    【解决方案1】:

    您应该在您的 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进行查询。

    【讨论】:

    • 谢谢,这解决了错误 - 但是,我的数据库仍然没有更新显示名称的新值。 update 函数是调用 mongodb 的更新还是只是我刚刚创建的用户实例?
    • 另外,这是我在 /app/models/user 中的架构: var userSchema = mongoose.Schema({ local : { email : String, password : String, displayName : String }, google : { id :字符串,令牌:字符串,电子邮件:字符串,名称:字符串,显示名称:字符串 } });
    • 谢谢,现在已修复!我还犯了另一个错误 - 我是基于 req.user 进行渲染的,这仍然是 POST 中未更改的记录。
    • @user1871200,我很高兴你的问题解决了。哦,是的,你是对的!
    猜你喜欢
    • 2017-06-24
    • 1970-01-01
    • 2016-07-04
    • 1970-01-01
    • 1970-01-01
    • 2022-08-18
    • 1970-01-01
    • 1970-01-01
    • 2021-01-30
    相关资源
    最近更新 更多