更新Node js + Express + Passport + MongoDB中的用户记录 [英] Updating a user record in Node js + Express + Passport + MongoDB

查看:60
本文介绍了更新Node js + Express + Passport + MongoDB中的用户记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,所以我已经为此苦苦挣扎了几个小时,无法弄清楚如何使其工作.

Ok, so I've been struggling with this for hours and can't figure out how to get it to work.

我按照本教程完成了使用mongodb,express和passport设置nodejs的操作: https://scotch.io/tutorials/easy-node-authentication-setup和本地 效果很好,除了现在我要使用经过身份验证的会话以允许用户通过表单创建显示名称.

I followed this tutorial to completion on setting up nodejs with mongodb, express and passport: https://scotch.io/tutorials/easy-node-authentication-setup-and-local It works great, except now I want to use the authenticated session to allow the user to create a display name via a form.

我很困惑,因为我不知道如何使用route/passport/session方法通过POST请求来更新mongodb中的用户记录.

I'm stumped because I can't figure out how to use the route/passport/session method to update the user record in mongodb via a POST request.

我的问题与下面的"Process Update Profile"代码块有关.我正在尝试使用user.update函数将显示名称添加到我的用户记录中.我收到以下错误:

My question relates to the 'Process Update Profile' block of code below. I'm trying to use the user.update function to add a displayname to my user record. I get the following error:

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)

我可以看到用户没有被传递给我正在使用的功能,但是我无法弄清楚应该使用哪种正确的方法-或如果我正以正确的方式进行操作. 参见下面的代码:

I can see that user isn't passed to the function I'm using, but I can't figure out what the correct method should be - or if I'm approaching this the right way at all. See code below:

// 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){

}; 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('/');

}

推荐答案

您应将此行添加到route.js文件的顶部:

You should add this line at the top of your routes.js file:

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模型的名称.

because when you use update function, the user in code below, should be the name of your MongoDB Model.

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进行查询.

if you want to find a user based on it's id, then you should query with the user's id.

这篇关于更新Node js + Express + Passport + MongoDB中的用户记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆