如何设置useMongoClient(Mongoose 4.11.0)? [英] How to set useMongoClient (Mongoose 4.11.0)?

查看:105
本文介绍了如何设置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".

你们知道怎么做吗?

推荐答案

没有打字稿,您几乎可以忽略该问题并使用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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆