Chuck
Chuck

Reputation: 258

UnhandledPromiseRejectionWarning: | DeprecationWarning: | Mongoose 5

In building a basic practice Chat app in node.js I've come across the above issue.

My express.js code :

var mongoose = require('mongoose')
var dbUrl = 'mongodb://ChatbotAdmin:[email protected]:39177/learning_node'
mongoose.connect(dbUrl,  (err) => {
console.log('Connected')
})

This is the full error:

(node:10192) UnhandledPromiseRejectionWarning: Unhandled promise rejection 
(rejection id: 1): MongoNetworkError: connection 0 to 
ds239177.mlab.com:39177 closed
(node:10192) [DEP0018] DeprecationWarning: Unhandled promise rejections are 
deprecated. In the future, promise rejections that are not handled will 
terminate the Node.js process with a non-zero exit code.

I tried adding {useMongoClient: true}, as did this fellow The options [useMongoClient] is not supported . Only to find as he did that in mongoose 5, its not necessary (and doesn't help).

I further researched the addition of :

mongoose.Promise = global.Promise

I had the same error.

This Question also did not help.

I would just fallback on an earlier version of mongoose but I'm curious to see what the solution will be...

Upvotes: 2

Views: 3183

Answers (5)

Zadiki Hassan Ochola
Zadiki Hassan Ochola

Reputation: 778

I had some how the same issue.This issue tend to appear whenever i use mlab mongodb but ok whenever i use my local db.I passed some options for tuning mongoose as described by mongoose official website https://mongoosejs.com/docs/connections.html const options = { useNewUrlParser: true, autoIndex: false, // Don't build indexes reconnectTries: Number.MAX_VALUE, // Never stop trying to reconnect reconnectInterval: 500, // Reconnect every 500ms poolSize: 10, // Maintain up to 10 socket connections // If not connected, return errors immediately rather than waiting for reconnect bufferMaxEntries: 0, connectTimeoutMS: 10000, // Give up initial connection after 10 seconds socketTimeoutMS: 45000, family: 4 // Use IPv4, skip trying IPv6 }; mongoose.connect(my_mongo_url,options); somehow that solved it for me

Upvotes: 1

Charlie
Charlie

Reputation: 183

How about you wrap your code in a try catch?

 var mongoose = require('mongoose')
 var dbUrl = 'mongodb://ChatbotAdmin:[email protected]:39177/learning_node'

 try {
   mongoose.connect(dbUrl, { useMongoClient: true })
 } catch(e) { console.log(e.message) } 

Upvotes: 0

Cavin
Cavin

Reputation: 1

Use this:

{ useNewUrlParser: true }

instead of:

useMongoClient: true

Upvotes: 0

Manjeet Singh
Manjeet Singh

Reputation: 2398

try this code

var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/test');
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function callback () {
  console.log("h");
});

//----------------------------
 Second option

// connect to  mongoose
mongoose.connect('mongodb://localhost/peppino-calc', {
  useMongoClient: true
})
.then(() => { console.log('MongoDB connected...')})
.catch(err => console.log(err));

Upvotes: 0

dasersoft
dasersoft

Reputation: 155

You should get a third party promise library like bluebird. See this below:

mongoose.Promise = require('bluebird');
DBURL = process.env.DBURL;

var options = {
useMongoClient: true,
  socketTimeoutMS: 0,
  keepAlive: true,
  reconnectTries: 30
};

mongoose.connect(DBURL, options);
db = mongoose.connection;
db.on('error', err => {
  console.log('There was a db connection error');
});
db.once('connected', () => {
  console.log('Successfully connected to ' + DBURL);
});
db.once('disconnected', () => {
  console.log('Successfully disconnected from ' + DBURL);
});
process.on('SIGINT', () => {
  mongoose.connection.close(() => {
    console.log('dBase connection closed due to app termination');
    process.exit(0);
  });
});

Bluebird will help you remove the deprecation error. I hope you find this helpful

Upvotes: 1

Related Questions