I tried to Promisify Mongoose connect by using bluebird, I need to reduce my callbacks so I used bluebird.But It gives me the error below.
var expect = require('chai').expect;
var mongoose = require('mongoose');
var jobModel = require('../models/job');
var Promise = require('bluebird');
function resetJobs() {
return new Promise(function(resolve, reject) {
mongoose.connection.collections['jobs'].drop(resolve, reject);
});
};
function findJobs(query) {
return Promise.cast(mongoose.model('Job').find({}).exec());
};
var connectDB = Promise.promisify(mongoose.connect,mongoose);
describe('get jobs', function() {
it('Should not be empty since jobs are seeded', function(done) {
connectDB('mongodb://localhost/jobfinder').then(function() {
resetJobs()
.then(jobModel.seedJobs)
.then(findJobs).then(function(jobList) {
expect(jobList.length).to.be.at.least(1);
done();
});
});
});
});
But this gives me a error
Unhandled rejection TypeError: Cannot read property 'connection' of undefined
at Mongoose.connect (F:\MyProjects\JobFinder\node_modules\mongoose\lib\index.js:232:18)
at tryCatcher (F:\MyProjects\JobFinder\node_modules\bluebird\js\release\util.js:11:23)
at ret (eval at <anonymous> (F:\MyProjects\JobFinder\node_modules\bluebird\js\release\promisify.js:184:12), <anonymous>:14:23)
at Context.<anonymous> (F:\MyProjects\JobFinder\test\jobs-data-spec.js:22:3)
The versions of packages I'm using as follows
"bluebird": "^3.1.1",
"express": "^4.13.4",
"mongoose": "^4.3.6"
I am working on the same tutorial.
Bluebird changes the api in 3.0 from
// 2.x Promise.promisify(fn, ctx);
// 3.0 Promise.promisify(fn, {context: ctx});
I made the call change and the calls stopped throwing the errors.
See here for the Bluebird explanation: http://bluebirdjs.com/docs/new-in-bluebird-3.html
Hope this helps