如何设置 useMongoClient (Mongoose 4.11.0)? [英] How to set useMongoClient (Mongoose 4.11.0)?
问题描述
这是我用来连接到我的数据库的代码:
This is the code I use to connect to my database:
private connectDatabase(databaseUri: string): Promise<Mongoose.Connection> {
return Mongoose.connect(databaseUri).then(() => {
debug('Connected to MongoDB at %O', databaseUri);
return Mongoose.connection;
});
}
今天我将 Mongoose 更新到了 4.11.0 版,并且在运行测试时收到了这个警告:
Today I updated Mongoose to version 4.11.0 and I got this warning when running my tests:
(node:4138) DeprecationWarning: `open()` is deprecated in mongoose >= 4.11.0,
use `openUri()` instead, or set the `useMongoClient` option if using `connect()`
or `createConnection()`
我找不到有关如何设置 useMongoClient"的任何信息.
I can't find any information on how to "set useMongoClient".
你们知道怎么做吗?
推荐答案
没有 Typescript 你几乎可以忽略这个问题并使用 Mongoose.connect(databaseUri, { useMongoClient: true })
.
Without Typescript you can pretty much ignore the issue and use Mongoose.connect(databaseUri, { useMongoClient: true })
.
如果您真的想避免出现警告,请避免使用 4.11.0 版.
If you really want to avoid having the warning avoid the version 4.11.0.
我更新到了 4.11.1 版,因为@types/mongoose@4.7.18 还没有更新,他们没有提到 ConnectionOptions
中的 useMongoClient
字段,这就是我导入模块的方式:
I updated to version 4.11.1 and since @types/mongoose@4.7.18 are not yet updated and they do not mention the field useMongoClient
in the ConnectionOptions
, this is how i imported the module:
const Mongoose = require('mongoose');
然后使用这个函数:
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}`));
}
这篇关于如何设置 useMongoClient (Mongoose 4.11.0)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!