使用 winston-mongodb 后收到警告 [当前服务器发现和监控引擎已弃用]
Posted
技术标签:
【中文标题】使用 winston-mongodb 后收到警告 [当前服务器发现和监控引擎已弃用]【英文标题】:getting the warning [current Server Discovery and Monitoring engine is deprecated] after using winston-mongodb 【发布时间】:2020-05-12 20:20:03 【问题描述】:安装winston-mongodb
后,我收到以下警告:
(node:9316) DeprecationWarning: 当前服务器发现和 监控引擎已弃用,未来将被移除 版本。要使用新的服务器发现和监控引擎,请通过 选项 useUnifiedTopology: true 到 MongoClient 构造函数。
这是我的 app.js:
require('express-async-errors');
const express = require('express'),
config = require('config'),
morgan = require('morgan'),
helmet = require('helmet'),
winston = require('winston'),
mongoose = require('mongoose');
require('winston-mongodb');
mongoose.connect("mongodb://localhost:27017/wsep",
useNewUrlParser: true, useFindAndModify: false, useCreateIndex: true, useUnifiedTopology: true )
.then(connect => console.log("connected to mongo db...") )
.catch(error => console.log("could not connect to mongo db ...") )
winston.add(new winston.transports.MongoDB(
db: 'mongodb://localhost:27017/wsep'
));
如果我删除/评论以下 sn-p 代码,上述警告将消失:
winston.add(new winston.transports.MongoDB(
db: 'mongodb://localhost:27017/wsep'
));
编辑:
package.json
"name": "backend-api",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts":
"dev": "nodemon app.js",
"start": "node app.js"
,
"engines":
"node": "13.5.0"
,
"keywords": [],
"author": "",
"license": "ISC",
"dependencies":
"bcrypt-nodejs": "0.0.3",
"compression": "^1.7.4",
"config": "^3.2.5",
"express": "^4.17.1",
"express-async-errors": "^3.1.1",
"helmet": "^3.21.2",
"joi": "^14.3.1",
"jsonwebtoken": "^8.5.1",
"jwt-token-encrypt": "^1.0.4",
"lodash": "^4.17.15",
"mongoose": "^5.8.9",
"multer": "^1.4.2",
"winston": "^3.2.1",
"winston-mongodb": "^5.0.1"
,
"devDependencies":
"nodemon": "^2.0.2"
【问题讨论】:
【参考方案1】:您可以将useUnifiedTopology: true
添加到winston-mongodb 的选项块中
像这样:
winston.add(new winston.transports.MongoDB(
db: 'mongodb://localhost:27017/wsep',
options:
useUnifiedTopology: true,
));
【讨论】:
【参考方案2】:将useUnifiedTopology:true
传递给对我有用的选项。请参阅下面的示例:
// Connect to Mongo
mongoose.set("useNewUrlParser", true);
mongoose.set("useUnifiedTopology", true);
mongoose
.connect(db)
.then(() => console.log("MongoDB Connected..."))
.catch(() => console.log(err));
【讨论】:
以上是关于使用 winston-mongodb 后收到警告 [当前服务器发现和监控引擎已弃用]的主要内容,如果未能解决你的问题,请参考以下文章
在 UICollectionView iOS 7 中收到内存警告
Mongoose:在添加 useNewUrlParse:true 等选项后,仍然收到来自 mongoose 模型 FindByIdAndUpdate() 的警告