Fez Vrasta Fez Vrasta - 4 months ago 11
Node.js Question

ensureIndex asks for callback

I need to create an index in MongoDB to store unique slugs.

I use this code to generate the index:

this._db = db;
this._collection = this._db.collection("Topics");
this._collection.ensureIndex( { slug: 1 }, { unique: true });


But when I run my tests it fails on the "beforeEach": (I'm using mongo-clean NPM)

beforeEach(function (done) {
clean(dbURI, function (err, created) {
db = created;
instance = topicManager(db);
done(err);
});
});


Saying:

Uncaught Error: Cannot use a writeConcern without a provided callback


What am I doing wrong? (if I comment the ensureIndex everything works)

Answer

As the response implies, you might need to provide a callback function to your call to ensureIndex:

this._db = db;
this._collection = this._db.collection("Topics");
this._collection.ensureIndex( { slug: 1 }, { unique: true }, function(error) {
  if (error) {
   // oops!
  }});
Comments