Quantcast
Viewing all articles
Browse latest Browse all 271

Can't display user data from database

I want to get data from database and display it in html code through <%= %> tag.

// index.jsconst express = require('express');const router = express.Router();const { ensureAuthenticated } = require('../config/auth');router.get('/dashboard', ensureAuthenticated, (req, res) =>  res.render('dashboard', {    user: req.user  }));module.exports = router;
// auth.jsmodule.exports = {    ensureAuthenticated: function(req, res, next) {        if(req.isAuthenticated()) {            return next();        }        req.flash('error_msg', 'Please log in to view this resource');        res.redirect('/users/login');    }};
     passport.serializeUser(function(user, done) {    done(null, user.id);  });  passport.deserializeUser(function(id, done) {    User.findById(id, function(err, user) {      done(err, user);    });  });

Example

// dashboard.ejs<p class="lead mb-3">Welcome <%= user.name %></p>

I don't get any error but output is just "Welcome" without users name, i am using <%= %> tag in other places and it is working fine and in app.js i am usingapp.use('/', require('./routes/index'));and it gets me to page it is supposed to.And when i remove ensureAuthenticated parts, outcome is the same.

When i change code to

router.get('/dashboard', (req, res) =>  res.render('dashboard', {    name: req.user.name  }));

with <%= name %> , it doesn't work either

I am new to this so sorry if i did not explain it well


Viewing all articles
Browse latest Browse all 271

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>