Tia*_*olo 52 mongoose mongodb node.js
这是我用来连接数据库的代码:
private connectDatabase(databaseUri: string): Promise<Mongoose.Connection> {
return Mongoose.connect(databaseUri).then(() => {
debug('Connected to MongoDB at %O', databaseUri);
return Mongoose.connection;
});
}
Run Code Online (Sandbox Code Playgroud)
今天我将Mongoose更新到4.11.0版本,运行我的测试时收到了这个警告:
(node:4138) DeprecationWarning: `open()` is deprecated in mongoose >= 4.11.0,
use `openUri()` instead, or set the `useMongoClient` option if using `connect()`
or `createConnection()`
Run Code Online (Sandbox Code Playgroud)
我找不到任何关于如何"设置useMongoClient"的信息.
你们知道怎么样吗?
Xer*_*rtz 77
这是你使用useMongoClient的方法:
mongoose.connect('mongodb://localhost/advisorDemoTestDB', { useMongoClient: true })
小智 29
添加{useMongoClient:true} 作为connect或createConnection方法的另一个参数,它取决于您使用的mongoose版本.
// Using `mongoose.connect`...
var promise = mongoose.connect('mongodb://localhost/myapp', {
useMongoClient: true,
/* other options */
});
// Or `createConnection`
var promise = mongoose.createConnection('mongodb://localhost/myapp', {
useMongoClient: true,
/* other options */
});
Run Code Online (Sandbox Code Playgroud)
小智 12
mongoose.connection.openUri('mongodb://127.0.0.1/camp_v12')
Run Code Online (Sandbox Code Playgroud)
有没有人试过这个?当我使用它时,我的弃用警告消失了,它来自文档
http://mongoosejs.com/docs/connections.html
小智 9
最简单的解决方法是打开终端,将目录更改为根项目(文件夹所在的package.json位置)
跑:
npm remove mongoose
然后:
npm install mongoose@4.10.8 --save
问题解决了.
升级并不总是最佳选择.
http://mongoosejs.com/docs/connections.html
没有Typescript你几乎可以忽略这个问题并使用它Mongoose.connect(databaseUri, { useMongoClient: true }).
如果你真的想避免让警告避免版本4.11.0.
我更新到4.11.1版本,因为@类型/猫鼬@ 4.7.18尚未更新,他们不提现场useMongoClient的ConnectionOptions,我这是怎么导入模块:
const Mongoose = require('mongoose');
Run Code Online (Sandbox Code Playgroud)
然后使用此功能:
private connectDatabase(databaseUri: string): Promise<any> {
return Mongoose.connect(databaseUri, { useMongoClient: true })
.then(() => {
console.log('Connected to MongoDB at ', databaseUri);
return Mongoose.connection;
})
.catch(err => debug(`Database connection error: ${err.message}`));
}
Run Code Online (Sandbox Code Playgroud)
正如许多答案所说,{ useMongoClient: true }在options connect或createConnectionmethod中添加options参数将解决此问题。
例如
// With mongoose.connect method
mongoose.connect('mongodb://localhost/app', { useMongoClient: true });
// With createConnection method
mongoose.createConnection('mongodb://localhost/app', { useMongoClient: true });
Run Code Online (Sandbox Code Playgroud)
但是MongoClient首先是什么?
从MongoDB Node.js驱动程序1.2版开始,引入了一个新的连接类MongoClient,该连接类在所有正式驱动程序中均具有相同的名称。
与已关闭确认的现有数据库连接类相比,新的连接类MongoClient确认所有对MongoDB的写入。
因此,{ useMongoClient: true }告诉Moongoose使用新的连接类而不是旧的连接类
欲了解更多信息,请点击这里
| 归档时间: |
|
| 查看次数: |
39313 次 |
| 最近记录: |