node.js - Mongo DB updated, but old info rendered -


i new using mean stack, , trying implement user service. want user able edit information such display name.

with following code, can see in mongo database change being made correctly, when log req.user, see displaying non-updated version, , update not being rendered.

please help!

var router = require('express').router(); router.route('/users/:user_id/changename')     // update user's display name     .post(function(req, res) {         user.findbyidandupdate(req.params.user_id, { $set: { name: req.body.newname }}, function(err, user) {             console.log(req.user);             console.log(req.body.newname);             res.render('profile.ejs', { user: req.user });         });     }); 

you didn't include information how req.user being set, if you're hitting user collection populate req.user each request, you'll have date user next request*.

if res.render using user object returned in callback, you'd see updated user:

res.render('profile.ejs', { user: user }); 

* in practice, wouldn't want hit user collection every request. ideally, you'd have session middleware cache user object duration of user's session. in case, you'd need update session's reference user after updating document in mongo.


Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -