Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7569877
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T15:16:35+00:00 2026-05-30T15:16:35+00:00

I’m trying to create a route for users to be able to check other

  • 0

I’m trying to create a route for users to be able to check other user’s profiles. However, I want these profiles to be accesed via 2 different urls /profile/nickname and /profile/id so that a profile can be accesed by using either the user’s 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 +"'s Profile",
            jsf:[],
        }
        });
    }
    else
    {
        res.render('404.jade', { 
            status: 404,
            title: 'Page Not Found', 
                jsf: []  
        });
    }
});
});

The problem is, it seems like it is only working with the id and not with the nickname, meaning that if I acces /profile/4f4ae474546708b219000005 things work, but if I access /profile/mmellad which is the given nickname for that user, I get the 404 page.

There is also one more thing I figured out that works fine for the nicknames, which is changing the query from

User.findOne( { $or : [{ "nickname": req.params.id },{ "_id": req.params.id }] }

to

User.findOne( { "nickname": req.params.id } }

in this case /profile/mmellado works fine but using the user id obviously doesn’t .

What would be the right way to do this? I’m thinking I may be using a wrong approach.

Another thing to mention is that if I try the following code in the mongo console, it works fine as well:

x = db.users.findOne({ $or: [ {nickname:"mmellado"}, {_id:ObjectId("4f4ae474546708b219000005")}  ]})

I tested that code by inserting the right nickname and a wrong _id, then tested with a wrong nickname and right _id. In both cases, x ended up containing the object for the record I needed.

I think I may be able to fix it with an additional route, but I’m new to Node.js and Express all together so I’m not sure what the propper approach would be.

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T15:16:36+00:00Added an answer on May 30, 2026 at 3:16 pm

    Have you tried this in console: x = db.users.findOne({ $or: [ {nickname:"mmellado"}, {_id:ObjectId("mmellado")} ]}). You’ll see an error even if nickname matches because it first tries to convert “mmellado” into an ObjectId and fails. This is might be the reason your page fails when using nickname.

    I don’t know the node.js mongodb-driver internals, but it probably tries to convert the argument internally to ObjectId before querying for “_id” field (and fail if not valid). I didn’t check this, so try it out. Also try checking the err object on the callback. If that is the problem one way to solve this is to check is argument is valid ObjectId before querying , for example create ObjectId out of it yourself and catch exception if it fails. Something like this (not tested):

    try {
      var objectId = createObjectId(req.params.id); // Create this function yourself
      User.findOne( { "_id": objectId }, callbackFunction );
    } catch (err) {
        // Fall back to using nickname
        User.findOne( { "nickname": req.params.id }, callbackFunction );
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically, what I'm trying to create is a page of div tags, each has
I'm trying to create an if statement in PHP that prevents a single post
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have a French site that I want to parse, but am running into
I want use html5's new tag to play a wav file (currently only supported
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I need to clean up various Word 'smart' characters in user input, including but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.