Mongoose error: `open ()` is deprecated in mongoose> = 4.11.0,

I'm on cloud 9 doing a web developer course that tries to run this code:

var mongoose = require('mongoose');
mongoose.connect("mongodb://localhost/cat_app");


var catSchema = new mongoose.Schema({
    name: String,
    age: Number,
    temperament: String
});

var Cat = mongoose.model('Cat', catSchema);
//add a new cat to db
var george = new Cat({
    name: 'George',
    age: 11,
    temperament: 'Grouchy'
});

george.save(function(err, cat) {
    if(err) {
        console.log('Something went wrong!');
    }else {
        console.log('We just saved a cat to the DB: ');
        console.log(cat);
    }
});
//get all cats from DB and log each one

      

And I am getting this error:

`open()` is deprecated in mongoose >= 4.11.0, use `openUri()` instead, or set the `useMongoClient` option if using `connect()` or `createConnection()`. See http:    //mongoosejs.com/docs/connections.html#use-mongo-client
Mongoose: mpromise (mongoose default promise library) is deprecated, plug in your own promise library instead: http: //mongoosejs.com/docs/promises.html

events.js:141
      throw er; // Unhandled 'error' event
      ^
MongoError: failed to connect to server [localhost:27017] on first connect [MongoError: connect ECONNREFUSED 127.0.0.1:27017]
    at null.<anonymous> (/home/ubuntu/workspace/database/node_modules/mongoose/node_modules/mongodb/node_modules/mongodb-core/lib/topologies/server.js:328:35)
    at emitOne (events.js:77:13)
    at emit (events.js:169:7)
    at null.<anonymous> (/home/ubuntu/workspace/database/node_modules/mongoose/node_modules/mongodb/node_modules/mongodb-core/lib/connection/pool.js:280:12)
    at g (events.js:260:16)
    at emitTwo (events.js:87:13)
    at emit (events.js:172:7)
    at Socket.<anonymous> (/home/ubuntu/workspace/database/node_modules/mongoose/node_modules/mongodb/node_modules/mongodb-core/lib/connection/connection.js:177:49)
    at Socket.g (events.js:260:16)
    at emitOne (events.js:77:13)
    at Socket.emit (events.js:169:7)
    at emitErrorNT (net.js:1269:8)
    at nextTickCallbackWith2Args (node.js:458:9)
    at process._tickCallback (node.js:372:17)

      

I tried using mongoose 4.10.8 but then I got this err:

Mongoose: mpromise (mongoose default promise library) is deprecated, plug in your own promise library instead: http://mongoosejs.com/docs/promises.html

events.js:141
      throw er; // Unhandled 'error' event
      ^
MongoError: failed to connect to server [localhost:27017] on first connect [MongoError: connect ECONNREFUSED 127.0.0.1:27017]
    at null.<anonymous> (/home/ubuntu/workspace/database/node_modules/mongoose/node_modules/mongodb/node_modules/mongodb-core/lib/topologies/server.js:328:35)
    at emitOne (events.js:77:13)
    at emit (events.js:169:7)
    at null.<anonymous> (/home/ubuntu/workspace/database/node_modules/mongoose/node_modules/mongodb/node_modules/mongodb-core/lib/connection/pool.js:280:12)
    at g (events.js:260:16)
    at emitTwo (events.js:87:13)
    at emit (events.js:172:7)
    at Socket.<anonymous> (/home/ubuntu/workspace/database/node_modules/mongoose/node_modules/mongodb/node_modules/mongodb-core/lib/connection/connection.js:177:49)
    at Socket.g (events.js:260:16)
    at emitOne (events.js:77:13)
    at Socket.emit (events.js:169:7)
    at emitErrorNT (net.js:1269:8)
    at nextTickCallbackWith2Args (node.js:458:9)
    at process._tickCallback (node.js:372:17)

      

You can help? Thanks guys!!!

+3


source to share


2 answers


here is what i am using to fix this issue if you check mongoose doc:

http://mongoosejs.com/docs/connections.html#use-mongo-client

This deprecation is because the MongoDB driver has deprecated the API it is important for the mongoose connection logic to support MongoDB 3.6, see this github issue and this blog post for more details.

With useMongoClient, you can declare these parameters at the top level instead, without additional addition. Here's a list of all maintained

options.mongoose.connect(myUri, {
  socketTimeoutMS: 0,
  keepAlive: true,
  reconnectTries: 30
});

      



So this works for me (add useMongoClient: true for the above config),

var mongodbUri = "mongodb://localhost:27017/mltdp";
var options = {
  useMongoClient: true,
  socketTimeoutMS: 0,
  keepAlive: true,
  reconnectTries: 30
};

var db = mongoose.connect(mongodbUri, options);

      

+2


source


I had success with npm remove mongoose

thennpm install mongoose@4.10.8 --save



+2


source







All Articles