Home > Enterprise >  Mongoose connecting to database but still responding to .catch method after mongoose.connect?
Mongoose connecting to database but still responding to .catch method after mongoose.connect?

Time:01-04

So I am learning the basics of Nodejs and using Mongoose to connect to mongodb. I can successfully connect to database and perform get and post methods on database, but when trying to connect to database, the mongoose.connect method that returns a promise, is working with both .then() and .catch() and I am not sure why, since a promise can either return a resolve or a reject. resolve should call .then() and reject should call .catch()...

Line 16 and the highlighted line in console show my issue. Any guidance is appreciated. Thanks in advance.

enter image description here

CodePudding user response:

You are not handle the connect promise reject\resolve properly. Try and change your connect code to something like

mongoose
  .connect('mongodb://127.0.0.1:27017/vidly')
  .then(() => console.log('Connected to Mongoose...'))
  .catch(err => console.error('Could not connect to mongodb'));
  • Related