承诺 Mongoose Connect 时出现类型错误
Type Error in Promisifying Mongoose Connect
我尝试使用 bluebird 来 Promisify Mongoose connect,我需要减少我的回调,所以我使用了 bluebird.But 它给了我下面的错误。
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();
});
});
});
});
但这给了我一个错误
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)
我使用的包版本如下
"bluebird": "^3.1.1",
"express": "^4.13.4",
"mongoose": "^4.3.6"
我正在编写相同的教程。
Bluebird 将 3.0 中的 api 从
// 2.x Promise.promisify(fn, ctx);
// 3.0 Promise.promisify(fn, {上下文: ctx});
我更改了调用,调用停止抛出错误。
Bluebird 的解释请看这里:
http://bluebirdjs.com/docs/new-in-bluebird-3.html
希望对您有所帮助
我也在编写相同的教程,以下是我为解决该问题所做的工作。
npm uninstall bluebird
npm install --save bluebird@2.0
然后当你运行你的测试mocha
你应该通过。
下面的代码似乎对我有用。
var connectMongoose = Promise.promisify(mongoose.connect, {context: mongoose});
connectMongoose('MONGO_URL', mongoose)
.then(..)
您也可以手动承诺 mongoose.connect,而不使用 bluebird:
const mongoose = require('mongoose');
const { promisify } = require('util');
const connectMongoose = promisify((resolve, reject) => {
const options = { useNewUrlParser: true, useUnifiedTopology: true };
const uri = 'mongodb+srv://<USERNAME>:<PASSWORD>' +
'@cluster0.iz4o8.mongodb.net/<DB-NAME>?retryWrites=true&w=majority';
try {
mongoose.connect(uri, options, resolve);
} catch (err) {
reject(err);
}
});
(async () => {
try {
await connectMongoose();
console.log('connected to monoose! :)');
}
catch (err) {
console.error('could not connect to mongoose :(', err);
process.exit(-1);
}
})();
我尝试使用 bluebird 来 Promisify Mongoose connect,我需要减少我的回调,所以我使用了 bluebird.But 它给了我下面的错误。
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();
});
});
});
});
但这给了我一个错误
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)
我使用的包版本如下
"bluebird": "^3.1.1",
"express": "^4.13.4",
"mongoose": "^4.3.6"
我正在编写相同的教程。
Bluebird 将 3.0 中的 api 从
// 2.x Promise.promisify(fn, ctx);
// 3.0 Promise.promisify(fn, {上下文: ctx});
我更改了调用,调用停止抛出错误。
Bluebird 的解释请看这里: http://bluebirdjs.com/docs/new-in-bluebird-3.html
希望对您有所帮助
我也在编写相同的教程,以下是我为解决该问题所做的工作。
npm uninstall bluebird
npm install --save bluebird@2.0
然后当你运行你的测试mocha
你应该通过。
下面的代码似乎对我有用。
var connectMongoose = Promise.promisify(mongoose.connect, {context: mongoose});
connectMongoose('MONGO_URL', mongoose)
.then(..)
您也可以手动承诺 mongoose.connect,而不使用 bluebird:
const mongoose = require('mongoose');
const { promisify } = require('util');
const connectMongoose = promisify((resolve, reject) => {
const options = { useNewUrlParser: true, useUnifiedTopology: true };
const uri = 'mongodb+srv://<USERNAME>:<PASSWORD>' +
'@cluster0.iz4o8.mongodb.net/<DB-NAME>?retryWrites=true&w=majority';
try {
mongoose.connect(uri, options, resolve);
} catch (err) {
reject(err);
}
});
(async () => {
try {
await connectMongoose();
console.log('connected to monoose! :)');
}
catch (err) {
console.error('could not connect to mongoose :(', err);
process.exit(-1);
}
})();