这是我用来连接数据库的代码:

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版,在运行测试时收到以下警告:

(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"的任何信息.

你们知道怎么做吗?

推荐答案

Without Typescript你几乎可以忽略这个问题,使用Mongoose.connect(databaseUri, { useMongoClient: true }).

如果你真的想避免出现警告,那就不要使用4.11.0版.

我更新到版本4.11.1,从@types开始/mongoose@4.7.18还没有更新,他们也没有提到ConnectionOptions中的字段useMongoClient,这就是我导入模块的方式:

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}`));
}

Node.js相关问答推荐

Spotify Auth访问令牌给出错误代码400

如何在Mongoose中调用动态Collection ?

我的Node.js应用程序没有将Mongoose方法findByIdAndDelete作为函数进行检测

通过PutObjectCommand上传AWS S3 PDF文件,结果为空PDF

mongodb首先自连接,然后根据某些条件与另一个表连接

使用 axios 和 Cheerio (Node js) 抓取 google 搜索

mongoose findOneAndUpdate 不更新数据库

如何在带有 JS 的 Nodejs 中使用没有 Async 方法的 Await

我应该转译我的 TypeScript 应用程序吗?

简单的 Node js 程序但执行顺序似乎不同?

nuxt:在 docker 镜像中找不到

部署云功能 Firebase/Stripe 时出错

如何在拦截器中发送不同的请求?

Ansible 将以什么用户身份运行我的命令?

使用 MongoDB 更新嵌套数组

Selenium WebDriver 等到元素显示

nodejs:Ajax 与 Socket.IO,优缺点

找不到在 docker compose 环境中运行的 node js 应用程序的模块

node.js 服务器和 HTTP/2 (2.0) 与 express.js

如何阻止 babel 将this转换为undefined(并插入use strict)