I've been working on a function that allows users to modify each other's data, including the ability to change roles. I'm having trouble getting the select form to display the current role of a user with the "selected" attribute.
This is what my edit.ejs
file looks like:
<select name="role" id="role">
<% roles.forEach((role) => { %>
<option value="<%= role._id %>" <% if(role._id === user.role._id) { %> selected <% } %>>
<%= role.name %>
</option>
<% }); %>
</select>
And this snippet is from my userController.js
file:
User.findById(req.params.id)
.populate('role')
.then((user) => {
Role.find().then((roles) => {
console.log(user);
res.render('pages/users/edit', {
title: 'Edit User',
roles: roles,
user: user,
});
});
});
Currently, none of the options in the select form are being marked as selected, despite the correct data showing up in the console log. Any help would be greatly appreciated! Thanks!