I'm currently attempting to display content of a collection in mongodb via a specific view. I would like to have it where if there is no content for said collection, it would just skip over but was told that's not needed with mongodb. However, when I attempt to run the following jade template, I get the error message listed above.
The js file in route that should be getting the data from the collection, empty or not.
router.get('/commentList', function(req, res) {
var db = req.db;
var comments = db.get('comments');
comments.find({},{},function(e,docs){
res.render('commentList', {
"commentList" : docs
});
});
});
and the code for the view in the accompying jade file
h2
Comments
ul
each comment, i in commentList
li
p comment.commentContent - comment.created_on - comment.author
the onyl thing I could see that could be causing the error is an empty collection but then how would you say "if collection is empty, do the following?" to avoid said error, assuming that's the problem?
EDIT: I'm staffed the database with content but still get the same error message, so at least it's not because the DB collection was empty.
EDIT #2: Got a question if comments was a mongo driver collection or mongoose. I'm not entirely sure (I think it's Mongoose as it created the connection), but this is how it is declared in app.js
var mongo = require('mongodb');
var Mongoose = require('mongoose');
var db = Mongoose.createConnection('localhost', 'acl');
EDIT #3: Tried changing the connection so it uses the mongo connection directly instead of through mongoose. Here's the code
var mongo = require('mongodb'),Server = mongo.Server,Db = mongo.Db;
var server = new Server('localhost', 27017, {safe:true});
var db = new Db('acl', server);
The error is still the same.
EDIT #4: I've scrapped this copy and have started anew due to my suspision that the cobbling of muultiple tutorials has made this a dog breakfest of code, and would be quicker to hit the reset button
Try this:
router.get('/commentList', function(req, res) {
req.db.get('comments').find({},{},function(e,docs){
res.render('commentList', {
commentList : docs || []
});
});
});
Given your code, try:
var mongoose = require('mongoose');
mongoose.model('comment', new Schema( {/*your data schema*/} );
router.get('/commentList', function(req, res) {
var db = req.db;
var comment = db.model('comment');
comment.find({}, function(e,docs){
res.render('commentList', {
"commentList" : docs
});
});
});
Reference: http://mongoosejs.com/docs/api.html#index_Mongoose-model
Update:
I added an explicit model definition. Ensure that your collection is called comments. To test this, you can open a mongodb shell and run: use ac1; db.comments.find({}) (I assume your db name is ac1) and make sure you're getting any data you have.
Also, you try changing your connection command to: mongoose.connect('mongodb://localhost/ac1');
Update 2:
You can check if the render is throwing an error by adding a callback:
res.render('commentList', {"commentList" : docs}, function( err, html ) {
//Inspect err.
});