5

I cannot for the life of me figure out why this doesn't finish:

var pmongo = require('promised-mongo');
var db = pmongo('mongodb://localhost/builder');
var block_id = '538d097bbb12479d0e9f70ab';

var collection = db.collection('block');
collection.findOne({_id:db.ObjectId(block_id)})
.then(function(result) {
    console.dir(result);
}).done();

It bascially just hangs. findOne returns a promise, I'm calling done. Strangely, when I close the database (ie db.close()) in the then, it finishes.

I'm trying to eventually make this handled via express, so I don't really want to close the database. What's the trick????

Nick Lang
  • 469
  • 6
  • 16
  • Why the extra `.done()` on the end with no arguments? For what you're doing, you only need a `.then()` or a `.done()`, not both. When you use both, you are chaining for multiple operations, but you don't have multiple operations. – jfriend00 Jun 04 '14 at 19:00
  • 1
    The trick is just to close the connection when you're done with it. Node will continue running as long as there is potential for another event to occur. And, as long as the connection is open, that potential is assumed. – Jonathan Lonowski Jun 04 '14 at 19:14
  • i put in lots of .done() hoping it would cause it to stop. seems weird. before I had promises, this would halt – Nick Lang Jun 04 '14 at 19:23
  • 1
    @NickLang it could be that `'promised-mongo'` is doing something else than original mongodb if original mongodb halts after the console.dir. Normal promisification doesn't affect such things at all. – Esailija Jun 05 '14 at 06:42

1 Answers1

6

MongoDB connections are intended to be persistent. You create one of them (or a pool of them) and then re-use that connection throughout your application.

This persistent network connection will keep the node.js process alive, so when you want to shut down the node process, you must manually close the connection. This is a common pattern with database connections and the same thing would happen if you were connecting to MySQL.

In an express.js application, just make the connection once, at the top of your file, then re-use that connection for every request.

ForbesLindesay
  • 10,482
  • 3
  • 47
  • 74
  • OP knows that, says the promise only executes if the connection is closed. – Benjamin Gruenbaum Jun 05 '14 at 10:39
  • That wasn't the impression I got. If adding db.close inside the `.then` call makes everything work then I think it's fair to assume the promise "executes". What I believe the OP means by "doesn't finish" is that the node.js program doesn't exit. My answer covers why that would be the case. – ForbesLindesay Jun 05 '14 at 12:36
  • Two possibilities: 1. it doesn't and something else was making it seem like it was (e.g. some other part of the program calling process.exit(0)) 2. It worked because the mongodb library that was being used for callbacks was opening and closing connections for every database request. While this is possible, it's not a good idea for performance. – ForbesLindesay Jun 05 '14 at 12:39