whats wrong with this call?

3,142 views
Skip to first unread message

Steve Portock

unread,
Jan 23, 2014, 4:00:42 PM1/23/14
to node-mong...@googlegroups.com
I run this code using node/mongo/ejs templates.

When i res.render the template, this code throws an exception, after the data gets returned to the screen.

When i send this back as just json, its fine.

I am using twitter bootstrap in this, for some reason that makes this error, when just plain html, no problems.

The error is 


/Users/steveportock/NodeApps/nodeRawMongo/node_modules/mongodb/lib/mongodb/mongo_client.js:413
              throw err
                    ^
Error: Argument passed in must be a single String of 12 bytes or a string of 24 hex characters
    at new ObjectID (/Users/steveportock/NodeApps/nodeRawMongo/node_modules/mongodb/node_modules/bson/lib/bson/objectid.js:33:11)
    at /Users/steveportock/NodeApps/nodeRawMongo/app.js:62:28
    at /Users/steveportock/NodeApps/nodeRawMongo/node_modules/mongodb/lib/mongodb/mongo_client.js:410:15
    at process._tickCallback (node.js:415:13)

The call is

//get one student
app.get('/students/:id', function(req, res){
mongoClient.connect(dbhost,function(err,db){
var collection = db.collection('Student');
console.log(req.params.id);
collection.findOne({_id: new ObjectID(req.params.id)},function(err,student){
if(!err) {
console.log(student);
//res.json({title:'student details',student:student});
res.render('studentdetails',{myTitle:'student details', student:student});
}
});
});
});


Any advice, critique, go look here would be appreciated, I have googled a bit, but am getting nowhere on this.

Steve

David Henderson

unread,
Jan 24, 2014, 5:22:54 AM1/24/14
to node-mong...@googlegroups.com
What's getting logged from console.log(req.params.id); ?

It looks like whatever is going through isn't a valid ObjectID. Maybe
add in some checking before it is passed into new ObjectID.

David
> --
> You received this message because you are subscribed to the Google Groups
> "node-mongodb-native" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to node-mongodb-na...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.



--
-- David Henderson

Lead Developer
Triggered Messaging Ltd
Real-Time Behavioural Personalisation.
Engage, Recover, Convert.
+44 (0)23 80 119 754

Kevin Miller

unread,
Jan 24, 2014, 7:14:08 AM1/24/14
to node-mong...@googlegroups.com
Not sure if this will help but place all of your key's in your json objects in quotes. Specifically student as it is the key and value for a few of your calls. 

Steve Portock

unread,
Jan 24, 2014, 9:01:32 AM1/24/14
to node-mong...@googlegroups.com
I believe its something in the ejs templates, if i don't use them its fine. I rewrote the pages using jade, it works perfectly.

I did try using quotes and no quotes with the call to find the _id, it works either way.

It has to be something in how the ejs templates do things, I was able to step thru the code using webstorm and even into the mongo client code, I could not see anything that would throw this off.

I had at one point console.log my json objects before rendering the page, the exception would occur after that, the objects were there.

My one main thing I wanted to know is what's the right way to handle the connection, and opening, closing, and getting things in an out of mongodb thru this, googling code examples yields way too many different ways of this, some work, some don't.

Even mongos docs are not quite right.

I will keep hacking on this, once I have it, I think I will post a full example of a simple crud app hitting common things you would need to do, there seems to be very little out there, just examples that take you just barely to getting this working, as if lots of guys just gave up lol.

I am a bit new to this, I got tired of .net and wanted to give this a try, no one told me it would be like coder crack lol.

If anyone can offer any other suggestions I am game for them.

Andrew Sednev

unread,
Jul 8, 2014, 5:01:26 AM7/8/14
to node-mong...@googlegroups.com
Hi!

You should replace this construction:
    new ObjectID(req.params.id)

To:
    new ObjectID.createFromHexString(req.params.id)
Reply all
Reply to author
Forward
0 new messages