简体   繁体   中英

Render data in ejs with mongoose

I am building a app where users can register and login, that part is working the data the user submit is stored in a MonboDB database with a Mongoose model.

Now I am trying to render data with ejs template engine. I fixed that I can render all the data from the database

.get('/dashboard', auth, async (req, res) =>  {
    try {
        const users = await User.find({}).lean()
        res.render('pages/dashboard', {users})
    } catch (err) {
        console.log(err)
        res.status(500).send('er ging iets mis')
    }
})

This is the user.js in my routes

And this is my dashboard.ejs

<%- include ('../partials/head') %>

<body>
<%- include ('../partials/header') %>
<% users.forEach((user, index) => { %>
    <p>Welkom <%= user.firstname %></p>
    <p>Leeftijd: <%= user.age %></p>
<% }) %>
<%- include ('../partials/nav') %>
</body>

This code renders everything from the database, so if I have 2 users in de database then I get back the firsname and age from both users. Now I only want to render the data from the user that is logged in. How can I do that?

My code is also on Github

What you currently have here:

.get('/dashboard', auth, async (req, res) =>  {
    try {
        const users = await User.find({}).lean()
        res.render('pages/dashboard', {users})
    } catch (err) {
        console.log(err)
        res.status(500).send('er ging iets mis')
    }
})

is executing a query to get all the users in the database and then passing the result into the template, that's why the page renders all the users you have. To render just the data of the user that is logged in, you should pass only the data of the user that is logged(not all users) into the template.

I checked the auth middleware from the linked repository and I can see that it checks for authorization by decoding a token(given to a user on login) stored in the request cookie. The middleware also takes care of finding(from the DB) the user associated with the request token ie the logged-in user, it stores this user in the request object as req.user . Consequently, a route that displays the data of a single user should be something like this:

.get('/userPage', auth, async (req, res) =>  {
    try {
        // The auth middleware already took care of fetching the 
        // data of the logged-in user
        const user = req.user;
        res.render('pages/userPage', {user})
    } catch (err) {
        console.log(err)
        res.status(500).send('er ging iets mis')
    }
})

I'm using a pages/userPage template in that route because the template your have for the dashboard is created to render a list of users, to render just a single user, you need to modify the dashboard template or just create a separate template for that. The template can be something like this:

<%- include ('../partials/head') %>

<body>
<%- include ('../partials/header') %>
<p>Welkom <%= user.firstname %></p>
<p>Leeftijd: <%= user.age %></p>
<%- include ('../partials/nav') %>
</body>

I hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM