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

标签 node.js mongodb express passport.js

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

我按照本教程完成了使用 mongodb、express 和 passport 设置 nodejs: https://scotch.io/tutorials/easy-node-authentication-setup-and-local 它工作得很好,除了现在我想使用经过身份验证的 session 来允许用户通过表单创建显示名称。

我很困惑,因为我不知道如何使用 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('/');

最佳答案

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

关于node.js - 在 Node js + Express + Passport + MongoDB 中更新用户记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38945382/

相关文章:

node.js - __dirname 和 ./in node.js 有什么区别?

node.js - 没有找到 gulpfile 错误但是我清楚地安装了 gulp

angularjs - 类型错误 : Dbschema(Mongoose Schema) is not a function

mongodb - 在集群外使用 StatefulSets 在 Kubernetes 上公开 MongoDB

Node.js - 使用 Express 获取原始请求正文

javascript - Node.js 中的 SPDY https.request()

javascript - d.ts 文件和 nodejs 需要具有相同名称的重复标识符错误

Mongodb 无法启动 -> 显示奇怪的错误日志

node.js - Mongoose 查找与执行。如何返回值?

node.js - Heroku Node.js 错误 heroku[路由器] : at=error code=H10 desc ="App crashed" method=GET path ="/"