DeprecationWarning: Listening to events on the Db class has been deprecated and will removed in the next major version

DeprecationWarning: Listening to events on the Db class has been deprecated and will be removed in the next major version

我正在使用 mongoose 和 mocha 进行 MongoDB 架构设计和 API 开发 我收到这个警告... 这是什么意思,它将如何影响我,解决方法是什么??

在实际警告文本下方:

(node:9872) DeprecationWarning: Listening to events on the Db class has been deprecated and will be removed in the next major version.

同样的错误

但据我所知这是一个新版本兼容性错误,在搜索当前错误版本后,我发现了这个 comment session 并且根据其中一个,忽略此警告是安全的。

更新

mongodb@3.6.5 出局了。

只需更新 mongodb 驱动程序和猫鼬:

npm i mongodb mongoose

这是由 mongoose 使用的 mongodb@3.6.4 native driver 引起的。

#1 您可以将 mongodb 降级到版本 3.6.3 ().

#2 或者将猫鼬从 5.11.16 降级回 5.11.15:

npm uninstall mongoose
npm install mongoose@5.11.15

#3 或者等待mongodb@3.6.5.

发布

您可以使用 mongoose 版本 5.11.13

如果您使用的是 mongoose 5.11.16 或更高版本,您将看到此错误。您可以通过将其降级到版本 5.11.15 来解决此问题。

npm uninstall mongoose

npm i mongoose@5.11.15

尽管社区和许多评论部分正在对此进行讨论,但由于存在兼容性错误,这是一个警告,忽略它可能无害。建议下次更新修复。

对于所有寻找此问题答案的人,Mongodb 论坛上发布了一个问题,Mongodb 相关人员已确认答案。

@kmgt 给出的答案是正确的。 可以安全地忽略该错误,并将在即将发布的 Mongodb Nodejs 驱动程序版本中修复,或者将 Mongoose 版本降级到 5.11.15 将有所帮助。

(node:44612) DeprecationWarning: Listening to events on the Db class has been deprecated and will be removed in the next major version

好的,所以我找到了解决此问题的方法。

任务:将图像上传到 mongoDB 作为二进制数据,使用桶、块等

Reference URL

代码:

const express = require("express");
const router = express.Router();
const User = require('../models/user');
const grid = require('gridfs-stream');
const GridFsStorage = require('multer-gridfs-storage');
const util = require("util");
const crypto = require('crypto');
const path = require('path');
const methodOverride = require('method-override');
const bodyParser = require('body-parser');
const dotenv = require("dotenv").config({path: "./config/config.env"});
const multer = require('multer');


const storage = new GridFsStorage({
    url: process.env.MONGO_URI,

    options: {
        useUnifiedTopology: true
    },
    file: (req, file) => {
        return new Promise((resolve, reject) => {
            crypto.randomBytes(16, (err, buf) => {
                if (err) {
                    return reject(err);
                }
                const filename = buf.toString('hex') + path.extname(file.originalname);
                const fileInfo = {
                    filename: filename,
                    bucketName: 'uploads'
                };
                resolve(fileInfo);
            });
        });
    }


});
var uploadFile = multer({storage: storage}).single("file");
var uploadFilesMiddleware = util.promisify(uploadFile);
module.exports = uploadFilesMiddleware;

Problem:

Deprecation Warning Listening to events on the Db class has been deprecated and will be removed in the next major version.

修复:

所以问题很可能不是因为你。它在包装本身中。 (查看上面的参考 link)

  1. 转到node_modules(就是那个文件夹里面有很多文件) node_js 应用程序中的文件夹。

  2. 转到 multer-gridfs-storage(在 node_modules 内)

  3. 转到 lib 文件夹(在 multer-gridfs-storage 内)

  4. 打开gridfs.js

  5. 找到这条评论(//这是所有发出错误的事件)

  6. 替换这个

    this.db .on('error', 错误事件) .on('parseError', 错误事件) .on('timeout', 错误事件) .on('close', errEvent);

有了这个

this.client
    .on('error', errEvent)
    .on('parseError', errEvent)
    .on('timeout', errEvent)
    .on('close', errEvent);

Basically change replace 'db' with 'client'.

你也可以去官方页面查看当前的问题那里明确提到 multer-gridfs-storage 有 debrication waring 问题。

Issue Link