Matt
Matt

Reputation: 186

Passing the value from controller to the template engine - MVC and EJS

I am trying to iterate the list of user and return it to the template engine. For some reason I got an error that object is not iterable.

This is my controller:

const Bug = require('../models/bugs');
const bodyParser = require('body-parser');
const User = require('../models/user');



exports.getAddBug = (req, res, next) =>{
    res.render('add-bug', {
        pageTitle: 'Add a bug',
        path: '/add-bug',
        user: req.session.user.fullName,
        allUsers : User.find()
    }

)};

There is a EJS file to which all the user values shall be passed:

<ion-item>
                            <% for (let user of allUsers) { %>
                            <ion-label>Assigned to</ion-label>
                            <ion-select name="assignedTo" placeholder="Select One">
                                <ion-select-option selected value="<%= user.fullName%>"><%= user.fullName%></ion-select-option>
                            </ion-select>
                            <% } %>
                        </ion-item>

Upvotes: 0

Views: 462

Answers (1)

Laurent Dhont
Laurent Dhont

Reputation: 1062

Returns your User.find() a promise? If so make your getAddBug an async function and await User.find() like this :

exports.getAddBug = async (req, res, next) =>{
    res.render('add-bug', {
        pageTitle: 'Add a bug',
        path: '/add-bug',
        user: req.session.user.fullName,
        allUsers : await User.find()
    }
)};

I hope this helped.

Upvotes: 1

Related Questions