0

I have a node.js application that uses a mongodb database that I've created. Within it, I have a simple collection named comments with the contents { "author": "me", "comment": "this is a comment" } when I call db.comments.find({}).

However, when I attempt to access this collection for display within a jade view I have, it times out after an incrediable amount of time. Console.log for the error object shows it's either a MongoError or connection was destroyed by application. The question I have is why this is happening? I have no errant while loops and connection parameteres seem to check out. Here's what I have to connect with, stored in app.js

var app = express();

var mongodb = require('mongodb'),
serverdb = new mongodb.Server('127.0.0.1', 27017, {}),
db = new mongodb.Db('acl', serverdb, {safe:true});

app.use(function(req,res,next){
    req.db = db;
    next();
});

and the code I have in the middleware file, stored as a js file in /routes

var express = require('express');
var router = express.Router();

/* GET home page. */
router.get('/', function(req, res) {
    var db = req.db;
    var collection = db.collection('comments');

    collection.find().toArray(function(err, docs) {
        console.log("Printing docs from Array");
        if (err) {
            console.log(err);
        } else {
            console.log(docs);
        }
    });

    db.close();
});

module.exports = router;
canadiancreed
  • 1,966
  • 6
  • 41
  • 58
  • 1
    Why are you closing the database for every request? – legalize Aug 09 '14 at 00:17
  • @legalize I believe I was doing it because I was under the impression it wouldn't show console.log data if the connection wasn't closed. Probably read it incorrectly somewhere in my travels. – canadiancreed Aug 09 '14 at 18:34

2 Answers2

1

Like @legalize said, its best to get a mongo connection pool going instead of opening and closing the connection on every request. Perhaps something like this SO answer

As far as why you are getting errors, its probably because your db.close() needs to be in the collection.find().toArray() callback because otherwise it'll start closing the connection before the query even happens.

Lastly, you need to render the template somewhere so the response gets sent back to the client.

Putting it all together, you probably want something like this:

router.get('/', function(req, res) {
    var db = req.db;
    var collection = db.collection('comments');

    collection.find().toArray(function(err, docs) {
        console.log("Printing docs from Array");
        db.close();
        if (err) {
            console.log(err);
        } else {
            console.log(docs);
            res.render( 'yourJadeTemplate', { docs : docs } );
        }
    }); 
});

(but you really don't want to be closing the connection for every request, especially because you aren't opening it for every request)

Community
  • 1
  • 1
go-oleg
  • 19,272
  • 3
  • 43
  • 44
  • I'll try the pool technique that you mention once I get the db working period. Figured get it working, then get it working well especially when starting out. So tried your code, still does the same thing, times out only this time none of the text in the console.log items are shown. – canadiancreed Aug 09 '14 at 18:43
0

Oddly enough replacing this code

var mongodb = require('mongodb'),
serverdb = new mongodb.Server('127.0.0.1', 27017, {}),
db = new mongodb.Db('acl', serverdb, {safe:true});

with this

var db = require("mongojs").connect("localhost:27017/acl", ["comments"]);

made all the difference. No more timeouts. A bit of tweeking to get it to return data.

canadiancreed
  • 1,966
  • 6
  • 41
  • 58