Обновление пользовательской записи в Node js + Express + Passport + MongoDB

Итак, я боролся с этим часами и не могу понять, как заставить его работать.

Я завершил этот урок по настройке nodejs с помощью mongodb, express и passport:https://scotch.io/tutorials/easy-node-authentication-setup-and-local Это прекрасно работает, за исключением того, что теперь я хочу использовать аутентифицированный сеанс, чтобы позволить пользователю создавать отображаемое имя через форму.

Я в замешательстве, потому что не могу понять, как использовать метод route / passport / session для обновления пользовательской записи в mongodb через запрос POST.

Мой вопрос относится к блоку кода «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)

Ваш ответ на вопрос