使用 winston-mongodb 后收到警告 [current Server Discovery and Monitoring engine is deprecated]
getting the warning [current Server Discovery and Monitoring engine is deprecated] after using winston-mongodb
安装 winston-mongodb
后,我收到以下警告:
(node:9316) DeprecationWarning: current Server Discovery and
Monitoring engine is deprecated, and will be removed in a future
version. To use the new Server Discover and Monitoring engine, pass
option { useUnifiedTopology: true } to the MongoClient constructor.
这是我的 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'
}));
如果我remove/comment下面的代码片段:
,上面的警告就会消失
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"
}
}
您可以将 useUnifiedTopology: true
添加到 winston-mongodb 的选项块中
像这样:
winston.add(new winston.transports.MongoDB({
db: 'mongodb://localhost:27017/wsep',
options: {
useUnifiedTopology: true,
}
}));
将 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
后,我收到以下警告:
(node:9316) DeprecationWarning: current Server Discovery and Monitoring engine is deprecated, and will be removed in a future version. To use the new Server Discover and Monitoring engine, pass option { useUnifiedTopology: true } to the MongoClient constructor.
这是我的 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'
}));
如果我remove/comment下面的代码片段:
,上面的警告就会消失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"
}
}
您可以将 useUnifiedTopology: true
添加到 winston-mongodb 的选项块中
像这样:
winston.add(new winston.transports.MongoDB({
db: 'mongodb://localhost:27017/wsep',
options: {
useUnifiedTopology: true,
}
}));
将 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));