Updating eines Benutzerdatensatzes in Node js + Express + Passport + MongoDB

Ok, also habe ich stundenlang damit zu kämpfen und kann nicht herausfinden, wie ich es zum Laufen bringen kann.

Ich habe dieses Tutorial zum Einrichten von NodeJS mit Mongodb, Express und Pass durchgearbeitet:https: //scotch.io/tutorials/easy-node-authentication-setup-and-loca Es funktioniert hervorragend, außer dass ich jetzt die authentifizierte Sitzung verwenden möchte, um dem Benutzer zu ermöglichen, einen Anzeigenamen über ein Formular zu erstellen.

Ich bin ratlos, weil ich nicht herausfinden kann, wie ich mit der Methode route / passport / session den Benutzerdatensatz in mongodb über eine POST-Anforderung aktualisieren kann.

Meine Frage bezieht sich auf den folgenden Codeblock 'Process Update Profile'. Ich versuche, die user.update-Funktion zu verwenden, um meinem Benutzerdatensatz einen Anzeigenamen hinzuzufügen. Ich erhalte den folgenden Fehler:

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)

Ich kann sehen, dass der Benutzer nicht an die von mir verwendete Funktion übergeben wurde, aber ich kann nicht herausfinden, wie die richtige Methode lauten sollte - oder ob ich mich dem überhaupt richtig nähere. Siehe Code unten:

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

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

}

Antworten auf die Frage(2)

Ihre Antwort auf die Frage