5

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!

2
  • What version of mongo are you using? Commented Feb 27, 2012 at 8:17
  • Mongo version is the following: Marcos-Mellados-iMac-2:~ marcos$ mongod --version db version v2.0.2, pdfile version 4.5 Mon Feb 27 02:48:11 git version: 514b122d308928517f5841888ceaa4246a7f18e3 Commented Feb 27, 2012 at 8:48

2 Answers 2

4

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 );
}
Sign up to request clarification or add additional context in comments.

6 Comments

I tried printing the error code at the query time. This is what I get: [Error: Invalid ObjectId], My assumption is that when I try to use Finch as a param, and the condition goes through the _id part, since the given string is not a valid ObjectId, the query breaks, returning an error... hence not giving the result even if part of the query did work... any ideas on how to work around this?
@MarcosMellado That seems to support my thoughts. I have proposed solution in my post, I modified it to add some sample code to make it more clear.
I had actually come to a VERY similar solution, I will do the proper changes for it to work with the try catch and let you know if it worked :)
The sample code makes sense to me, however, I get ReferenceError: ObjectID is not defined I'm not sure of the reason but this is what I've been trying to find out for a while... how to create an object id :S
After defining Schema = mongoose.Schema, ObjectId = Schema.ObjectId, in my app.js I get: 500 Error: This is an abstract interface. Its only purpose is to mark fields as ObjectId in the schema creation.
|
0

Default _id values are 12 byte binary hashes so you first need to convert the string into binary before you send the query using ObjectID.createFromHexString like this:

var id = ObjectID.createFromHexString(idHex);

Then use id in your query.

Your code should look something like this:

User.findOne( { $or : [{ "nickname": req.params.id },{ "_id": ObjectID.createFromHexString(req.params.id) }] }, function(err, user) { ........

Greetings Finch!!!

3 Comments

How would you define the ObjectId? Right now I have mongoose = require('mongoose'), ObjectID = mongoose.Schema.ObjectId,
Ignore the last comment, the problem with his approach is, at the moment of sending a nickname as a parameter and passing it through the createFromHexString function, it throws the next error: Error: Argument passed in must be a single String of 12 bytes or a string of 24 hex characters in hex format (assuming the nickname is finch for example)
Ture, try to first convert your string (Finch) into a Hex String using: var hexString = nickname.toHexString(); - then pass that as the argument to create your objectID

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.