user1871200 user1871200 - 3 months ago 40
Node.js Question

Updating a user record in 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.

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.

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.

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


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


}

Answer

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.

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

the correct query for update is:

{_id: req.session.passport.user.id}

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