mongoose.connection()和mongoose.createConnection()之间的混淆 [英] Confusion between mongoose.connection() and mongoose.createConnection()
问题描述
我研究猫鼬已经三天了,对这两种方法的使用我有点困惑(我知道将来会不推荐使用"mongoose.connection()" ...)
I've been studying mongoose for three days and I'm a bit confused about the use of these two methods (i know that "mongoose.connection()" will be deprecated in the future...)
问题是:当我尝试转换(从"mongoose.connection()"到"mongoose.createConnection()")时,此示例的action.js文件
The problem is: when I'm trying to convert (from "mongoose.connection()" to "mongoose.createConnection()") the action.js file of this example https://gist.github.com/2785463 it seems to not work for me...
有我的代码...
var mongoose = require('mongoose'),
db = mongoose.createConnection('localhost', 'test');
db.on('error', function () {
console.log('Error! Database connection failed.');
});
db.once('open', function (argument) {
console.log('Database connection established!');
mongoose.connection.db.collectionNames(function (error, names) {
if (error) {
console.log('Error: '+ error);
} else {
console.log(names);
};
});
});
还有我的终端输出(在我的ubuntu终端上键入"node test.js".)
and there's my terminal output (typing "node test.js" on my ubuntu terminal..)
Database connection established!
/home/_user_/Scrivania/node_modules/mongoose/node_modules/mongodb/lib/mongodb/connection/server.js:437
throw err;
^
TypeError: Cannot call method 'collectionNames' of undefined
at NativeConnection.<anonymous> (/home/_user_/Scrivania/test2.js:11:25)
at NativeConnection.g (events.js:192:14)
at NativeConnection.EventEmitter.emit (events.js:93:17)
at open (/home/_user_/Scrivania/node_modules/mongoose/lib/connection.js:408:10)
at NativeConnection.Connection.onOpen (/home/_user_/Scrivania/node_modules/mongoose/lib/connection.js:415:5)
at Connection._open (/home/_user_/Scrivania/node_modules/mongoose/lib/connection.js:386:10)
at NativeConnection.doOpen (/home/_user_/Scrivania/node_modules/mongoose/lib/drivers/node-mongodb-native/connection.js:47:5)
at Db.open (/home/_user_/Scrivania/node_modules/mongoose/node_modules/mongodb/lib/mongodb/db.js:287:14)
at Server.connect.connectCallback (/home/_user_/Scrivania/node_modules/mongoose/node_modules/mongodb/lib/mongodb/connection/server.js:235:7)
at g (events.js:192:14)
推荐答案
如果不调用mongoose.connect()
,则mongoose.connection
不包含打开的连接.您应该使用mongo.createConnection()
调用中的返回值(已保存到db
中).
If you don't call mongoose.connect()
then mongoose.connection
doesn't contain an an open connection. You should be using the return value from your mongo.createConnection()
call instead (that you've saved into db
).
因此,代码的最后一部分应更改为:
So the last section of code should change to:
已更新
db.db.collectionNames(function (error, names) {
if (error) {
console.log('Error: '+ error);
} else {
console.log(names);
};
});
我在Connection
上看不到collectionNames
方法;看来您必须遵循属性到本机连接对象中才能访问该对象(请参见上面的代码).
I don't see a collectionNames
method on Connection
; looks like you have to follow properties down into the native connection object to access that (see above code).
这篇关于mongoose.connection()和mongoose.createConnection()之间的混淆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!