I am trying to create a route for users to be able to check other user profiles. However, I want these profiles to be connected through 2 different URLs /profile/nicknameand /profile/idthat the profile can be retrieved using either the user nickname or user ID. I tried the following code:
app.get("/profile/:id", function(req, res) {
User.findOne( { $or : [{ "nickname": req.params.id },{ "_id": req.params.id }] }, function(err, user) {
if(user)
{
res.render('users/profile.jade', {
locals: {
currentUser: user,
title: user.nickname +" Profile",
jsf:[],
}
});
}
else
{
res.render('404.jade', {
status: 404,
title: 'Page Not Found',
jsf: []
});
}
});
});
The problem is that it only works with the identifier and not with the alias, which means that if I use /profile/4f4ae474546708b219000005everything works, but if I get access to /profile/mmelladwhich is the given alias for this user, I get page 404.
There is one more thing that I realized that works great for aliases that change the request from
User.findOne( { $or : [{ "nickname": req.params.id },{ "_id": req.params.id }] }
User.findOne( { "nickname": req.params.id } }
/profile/mmellado , , , , .
? , .
: mongo, :
x = db.users.findOne({ $or: [ {nickname:"mmellado"}, {_id:ObjectId("4f4ae474546708b219000005")} ]})
, _id, _id. x , , .
, , Node.js Express , , propper.
!