0

This might sound really easy. There is a very similar answer but somehow my code is behaving abruptly and driving me crazy. I have spent last 4-5 hours on this. Any help would be great.

I am building a Socket.IO chat app using Node + Express + Passport. I want to compare the userId of the current user with that of other users connected. I want to show him all users except himself to chat with in Socket.IO.

Read comments on code for clarity :

users.js

app.get('/users', function (req, res) {

var connectedUsers = require('../app').chatUsers; // =>  This is an array of all logged in user and I am getting this all correctly.

if (req.isAuthenticated()) {

    res.render('users', {
        currentUser: req.user,
        users: connectedUsers,
        title: 'OnLineUsers'

    });
}
});

users.ejs

<h3>CurrentUser : <%- currentUser.id %></h3> // => This Prints like **abcdefg**


<% for(var i = 0;i < users.length ; i++){ %>

<h3>UserID: <%- users[i].id %></h3> // => This also prints the same **abcdefg** if only one user is connected.

<% if( currentUser.id !== users[i].id){ %> // => But this Conditional Operator  does not works and show the current user himself.

<ul>
    <li>
        <h3> <%= users[i].name %></h3>
        <a href="/socket" id="chatRoomUrl">
            <button type="submit" class="btn-success">Chat</button>
        </a>
    </li>

</ul>

<% } %>
<% } %>

Thanks in advance. If there is any silly mistake on my part, please excuse me for wasting your time.

Community
  • 1
  • 1
KIA
  • 182
  • 2
  • 9

2 Answers2

0

Your code is fine. Maybe there is space in two property. Try :

currentUser.id.trim() !== users[i].id.trim()
Radian Jheng
  • 667
  • 9
  • 20
0

Change your following line, notice the change:

<h3> <%= users[i].name %></h3>

to this:

<h3> <%- users[i].name %></h3>
Danyal Shahid
  • 136
  • 1
  • 8