如何解决“警告:在mongoose 5.x中不再需要`useMongoClient`选项,请删除它。”

时间:2019-02-02 15:27:07

标签: node.js mongodb

我使用由nodejs制作的loginapp时遇到问题,但我不理解“删除usemongoclient”,因为我已经将其删除了!请帮助

完整的错误:

WARNING: The `useMongoClient` option is no longer necessary in mongoose 5.x, please remove it.
    at handleUseMongoClient (C:\Users\Corentin\node_modules\mongoose\lib\connection.js:610:17)
    at NativeConnection.Connection.openUri (C:\Users\Corentin\node_modules\mongoose\lib\connection.js:480:7)
    at Mongoose.connect (C:\Users\Corentin\node_modules\mongoose\lib\index.js:271:15)
    at Object.<anonymous> (C:\Users\Corentin\Documents\newLoginapp\models\user.js:3:10)
    at Module._compile (internal/modules/cjs/loader.js:689:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)
    at Module.load (internal/modules/cjs/loader.js:599:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
    at Function.Module._load (internal/modules/cjs/loader.js:530:3)
    at Module.require (internal/modules/cjs/loader.js:637:17)
    at require (internal/modules/cjs/helpers.js:22:18)
    at Object.<anonymous> (C:\Users\Corentin\Documents\newLoginapp\routes\users.js:6:12)
    at Module._compile (internal/modules/cjs/loader.js:689:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)
    at Module.load (internal/modules/cjs/loader.js:599:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
(node:15100) DeprecationWarning: current URL string parser is deprecated, and will be
removed in a future version. To use the new parser, pass option { useNewUrlParser: true } to MongoClient.connect.
Server started on port 3000
(node:15100) DeprecationWarning: collection.ensureIndex is deprecated. Use createIndexes instead.

这是我的代码:(不完整)要求提供更多代码

const express = require('express');
const path = require('path');
const cookieParser = require('cookie-parser');
const bodyParser = require('body-parser');
const exphbs = require('express-handlebars');
const expressValidator = require('express-validator');
const flash = require('connect-flash');
const session = require('express-session');
const passport = require('passport');
const LocalStrategy = require('passport-local').Strategy;
const mongo = require('mongodb');
const mongoose = require('mongoose');

mongoose.Promise = global.Promise;

mongoose.connect('mongodb://localhost/tuto', { useNewUrlParser: true })

var db = mongoose.connection;

const routes = require('./routes/index');
const users = require('./routes/users');

const app = express();

但是后端系统运行良好,我听不懂! ;'(

4 个答案:

答案 0 :(得分:1)

//Connect to mongoose (
mongoose.connect(db.mongoURI, {

     useNewUrlParser: true    //add it
    //useMongoClient: true //old

})
.then(() => console.log('Veri baglantisi saglandi...'))
.catch(err =>console.log(err));

答案 1 :(得分:0)

只需删除或更换

useMongoClient: true //remove it 

从数据库配置文件中删除,因为当前的猫鼬版本不支持此功能,只需在数据库配置文件中添加以下代码

useNewUrlParser: true    //add it

示例

//Connect to mongoose 
   exports.connect = () => {
  mongoose.connect(mongo.uri,{
    keepAlive: 1,
    useNewUrlParser: true,
  });
  return mongoose.connection;
};

答案 2 :(得分:0)

由于5.x猫鼬不再需要'useMongoClient'。 删除后,您必须按以下方式使用新的解析器,

  1. 对于新的URL解析器,请通过选项
'useNewUrlParser: true'
  1. 对于新的服务器发现和监视引擎,请通过选项
' useUnifiedTopology: true '

最终代码部分看起来像

mongoose.connect(
    'mongodb://localhost/tuto',
    {
        // useMongoClient: true // old parameter
        useNewUrlParser: true, // new parameters
        useUnifiedTopology: true
    }
);

希望它对最新的猫鼬版本更有帮助,例如:5.8.9。

答案 3 :(得分:0)

出现此警告是因为useMongoClient选项不再支持猫鼬新版本(> 5.x)。

解决方案已删除

useMongoClient: true //remove this code

并替换为

useNewUrlParser: true //add this code

示例

mongoose.connect('<<your uri>>', {
// useMongoClient: true
useNewUrlParser: true

});