0

I'm trying to check the values of 2 objects, one which the mongoose returns and one which passport js returns.

app.get('/user-area', isLoggedIn, function(req, res){
    User.find({}, function(err, users){
        if(err) throw err;

        res.render('admin/user-area/allUsers', {
            user: req.user,
            users: users,
            logged_user: req.user._id
        });
    });
});

in the above code, i'm parsing the values to the ejs file, on the ejs file i'm doing a simple comparison to filter out some data.

<ul>
    <%for(var i = 0; i < users.length; i++){%>
        <%if(logged_user != users[i]._id){%>
            <li><%=users[i].local.username%></li>
        <%}%>
    <%}%>
</ul>

I'm trying to filter out the logged in user from printing to the screen using the simple if statement, when printed on screen as a string, they seem to be exactly the same but for a reason I cannot find out, they fail on the if statement,

mastercordy
  • 161
  • 1
  • 2
  • 16

1 Answers1

1

My guess is that one is an ObjectId and the other is a String so they will never equal.

See this answer for comparing ObjectIds using equals():

https://stackoverflow.com/a/11638106/828573

Kilizo
  • 3,192
  • 4
  • 19
  • 20