node.js - Unable to render user object to EJS view header.ejs -


i new ejs , node.js. have been struggling send user object data ejs template page. actually, want display current user's name logged portal.

this portion of app.js

app.use(function(req,res,next){   res.locals.success_msg=req.flash('success_msg');   res.locals.error_msg=req.flash('error_msg');   res.locals.not_registered_user_msg=req.flash('not_registered_user_msg');   res.locals.authdata = firebase.auth().currentuser;   next(); }); //get user info app.get('*',function(req,res,next){   if(firebase.auth().currentuser!=null){       var id = firebase.auth().currentuser.uid;       var profileref = fbref.child('users');       var query = profileref.orderbychild('uid').equalto(id);       query.once('value', function(snap){       var user = json.stringify(snap.val());       console.log('user data is: '+user);       res.locals.user = user;      });    }   next();  }); 

here header.ejs want retrieve , display current user's name: enter image description here

i able enter if condition., , able print phrase "welcome,". unable actual user data, showing empty space enter image description here

please suggest me, did gone wrong here! in advance. per sugegstion adding logs console here. getting user data firebase including child id marked here. enter image description here

this doesn't right me:

var user = json.stringify(snap.val()); console.log('user data is: '+user); res.locals.user = user; 

you're converting user object json string. try instead:

var user = snap.val(); console.log('user data is: ' + json.stringify(user)); res.locals.user = user; 

i'm not familiar firebase i'd guess once asynchronous, you'd need wait before calling next, bit this:

if (firebase.auth().currentuser != null) {     // ... bit same before ...      query.once('value', function(snap) {         var user = snap.val();         console.log('user data is: ' + json.stringify(user));         res.locals.user = user;         next();     }); } else {     next(); } 

if still doesn't work, include output of console logging in question?


Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -