EJS just outputs the first found user in some cases


I’m Using mongoDB and ejs to display all my users in a table. The table also has some action buttons to delete the user and to change the users role. The buttons open a popup to change the role or to confirm the deletion. But EJS doesn’t pass the users info into the popup. It works totally fine in the table, but not in the popup.

My EJS User Table with the Role change Popup:

                    <td><span class="badge label-table badge-<%=users.role%>"><%=users.role%></span></td>
                    <td><span class="badge label-table badge-<%=users.verifyEmailToken%>"><%=users.verifyEmailToken%></span></td>
                      <button type="submit" class="btn btn-xs btn-success" data-toggle="modal" data-target="#con-close-modal" name="changeRoleButton"><i class="remixicon-user-settings-line"></i></button>

                  <div id="con-close-modal" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true" style="display: none;">
                    <div class="modal-dialog">
                      <div class="modal-content">
                        <div class="modal-header">
                          <h4 class="modal-title">Change <%=users.name%> Role</h4>
                          <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                        <form class="" action="/changeuserrole" method="post">
                          <div class="modal-body p-4">
                            <div class="row">
                              <div class="col-md-12">
                                <div class="form-group">
                                  <label for="field-1" class="control-label">User Role</label>
                                  <select class="form-control" name="role">
                                <button type="submit" value="<%=users._id%>" name="userroleid" class="btn btn-primary">Submit</button>

Here is my app.js where I search for the users and pass them to EJS:

    app.get("/users", function(req, res) {
    if (req.isAuthenticated() && req.user.role === "Admin") {

    User.find({}, function(err, foundUsers) {
      if (err) {
      } else {
        res.render("users", {
          users: foundUsers,
          name: req.user.name.replace(/ .*/, ''),
          email: req.user.username,
  } else {

All the <%=users…%> tags work inside the table, but not inside the popup divs. Inside the Popup it just displays the information from the first user in the Database, which is super strange.

I would be very thankful for any kind of help. Thanks!


Your ejs code is good. I think that the problem is the id of each modal.
For each user you generate a modal with id="con-close-modal", So all your modals have the same id. As a result, every submit button (all of them have the same data-target="#con-close-modal"), triggers the same modal, probably the first one.
I recommend you, give each modal a unique id like

<div id="<%= users._id %>" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true" style="display: none;">

and give each submit button the right data-target attribute

<button type="submit" ... data-target="#<%= users._id %>"...></button>

Answered By – dimitris tseggenes

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published