This is a race problem. The two functions that are extracted from MongoDB are asynchronous, so the call to res.render() occurs before the database returns data in each function of the corresponding callback. You need to enclose each function so that they have access to the appropriate context. See below:
app.get('/Post/:id', function (req, res, next){ Post.findOne({_id:req.params.id},function(err, postData){ if (err) return next(err); Comment.findOne({postid:req.params.id},function(err, comments){ if (err) return next(err); return res.render(__dirname+"/views/postdetail",{ title: 'adfasdf', stylesheet: 'postdetail', post:postData, comments:comments }); }); }); });
However, you can see how this can become quite dirty when you nest further and further. To prevent this, you can use a control flow library like caolan / async
Side note:
You Jade is looking for an iteration over the comments array, and you are returning a single document from MongoDB (assuming you are using the mongoose module). You will want to change your Mongoose function from findOne() to just find() so that mongoose can return an array of documents with the correct postid .
Edit:
Vinayak Mishra is also right in pointing out that you can use Express' middleware to impose a control flow on the route. Here is an example:
// Use the app.param() method to pull the correct post doc from the database. // This is useful when you have other endpoints that will require work on // a Post document like PUT /post/:postid app.param('postid', function (req, res, next, id) { Post.findById(id, function (err, post) { if (err) return next(err); if (!post) return next('route'); req.post = post; }); }); app.get('/post/:postid', // -- First route middleware grabs comments from post doc saved to req.post function (req, res, next) { Comment.find({ postid: req.post.id }, function (err, comments) { if (err) return next(err); req.comments = comments; next(); }); }, // -- This route middleware renders the view function (req, res, next) { res.render('/postDetail', { // ... list locals here ... }); } );