Aktualisieren einer Benutzer-Datensatz in Node js + Express + Pass + MongoDB

Ok, so ich ' ve wurde kämpfen mit diesem für Stunden und kann nicht herausfinden, wie man es an die Arbeit.

Ich folgte diesem tutorial zum Abschluss der Einrichtung nodejs mit mongodb, express-und Reisepass:
https://scotch.io/tutorials/easy-node-authentication-setup-and-local
Es funktioniert Super, außer ich will jetzt die authentifizierte Sitzung verwenden, um dem Benutzer zu ermöglichen, erstellen Sie einen display-Namen über ein Formular.

Bin ich überfragt, denn ich kann nicht herausfinden, wie die route - /Pass - /session-Methode zum aktualisieren der Benutzer-Datensatz in mongodb über eine POST-Anfrage.

Meine Frage bezieht sich auf den "Prozess" Profil Aktualisieren' code-block weiter unten. Ich versuche, den Benutzer.update-Funktion zum hinzufügen einen Anzeigenamen zu meinem Benutzer-Datensatz. Ich bekomme die folgende Fehlermeldung:

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)

Kann ich sehen, dass Benutzer nicht an die Funktion übergeben, die ich verwende, aber ich kann nicht herausfinden, was die richtige Methode sein sollte - oder wenn ich mich an dieses die richtige Art und Weise an alle.
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('/');

}

InformationsquelleAutor user1871200 | 2016-08-14
Schreibe einen Kommentar