添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
相关文章推荐
欢快的拐杖  ·  Mongoose v8.6.2: ...·  1 月前    · 
销魂的小熊猫  ·  Make Mongoose's ...·  2 月前    · 
重感情的水桶  ·  mongoose findOne - CSDN文库·  2 月前    · 
犯傻的铅笔  ·  Mongoose: ...·  2 月前    · 
帅气的木耳  ·  Mongoose v8.5.2: ...·  2 月前    · 
正直的冲锋衣  ·  周口市政府采购网·  1 月前    · 
聪明伶俐的青蛙  ·  NEC杯围棋赛决赛 ...·  2 月前    · 
淡定的鸭蛋  ·  学生电子交通卡申领·  3 月前    · 

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement . We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to handle mongoose.connect() error in catch handler? I want to use application initialization chain but can't do that because mongoose.connect() does not return rejected promise. It returns rejected promise only if I specify callback, but it's not a perfect solution.

Example:

mongoose.connect('mongodb://127.0.0.2/test') // if error it will throw async error
    .then(() => { // if all is ok we will be here
        return server.start();
    .catch(err => { // we will not be here...
        console.error('App starting error:', err.stack);
        process.exit(1);
    });

Workaround:

mongoose.connect('mongodb://127.0.0.2/test', function() { /* dummy function */ })
    .then(() => {
        return server.start();
    .catch(err => { // mongoose connection error will be handled here
        console.error('App starting error:', err.stack);
        process.exit(1);
    });

I think mongoose.connect() throws async error instead of return rejected promise in order to not break backward compatibility. Users expect that application will be finished with error code if something went wrong with mongoose connection establishment. If mongoose.connect() returns rejected promise application will be finished with 0 code and nothing will be output to console. So it will be good to have some way to say mongoose.connect() to return promise. Maybe something like exec() :

mongoose.connect('mongodb://127.0.0.2/test').exec()
    .then(() => { // if all is ok we will be here
        return server.start();
    .catch(err => { // if error we will be here
        console.error('App starting error:', err.stack);
        process.exit(1);
    });
mongoose.connect('mongodb://localhost/dbCollection', function(err, db) {
    if (err) {
        console.log('Unable to connect to the server. Please start the server. Error:', err);
    } else {
        console.log('Connected to Server successfully!');
          

Use the callback of mongoose.connect to catch any error during the connection.
You can start you server in the event open.

        mongoose.connection.once('open', function() {
            logger.info('MongoDB event open');
            logger.debug('MongoDB connected [%s]', url);
            mongoose.connection.on('connected', function() {
                logger.info('MongoDB event connected');
            });
            mongoose.connection.on('disconnected', function() {
                logger.warn('MongoDB event disconnected');
            });
            mongoose.connection.on('reconnected', function() {
                logger.info('MongoDB event reconnected');
            });
            mongoose.connection.on('error', function(err) {
                logger.error('MongoDB event error: ' + err);
            });
            // return resolve();
            return server.start();
        });
        return mongoose.connect(url, options, function(err) {
            if (err) {
                logger.error('MongoDB connection error: ' + err);
                // return reject(err);
                process.exit(1);
        });
afbayonac, bengitiger, Serg4554, lacivert, nixoncode, mmed, daxeh, SankaD, tomasevich, hansfpc, and 2 more reacted with thumbs up emoji IlyaEremin, larsfroelich, c-tn, davNazaryan, snalesso, badshaadon, sandrodesantis, depkchowdary, kamyarghajar, marcinlesek, and 2 more reacted with confused emoji All reactions

I have the same issue.

I'd like to be able to use mongoose.connect(...).catch(failCallback) but when an error occurs upon initial connection attempt failCallback does not execute. There's something wrong with the MongooseThenable pseudo-promise that mongoose.connect returns. Moreover I have configured mongoose.Promise = require('bluebird') and I really expect such async calls to return a real promise, a Bluebird one in my case.

The problem with my failCallback not executing, however, happens only initially, i.e.:

// First kill mongod instance so that Mongoose cannot connect and start the app
mongoose.connect(uri, options).catch(failCallback); // failCallback does _not_ get called
// Then stop the app, start mongod instance so that Mongoose can connect, start the app
mongoose.connect(uri, options).then(successCallback); // successCallback gets called
// Then kill mongod instance so that Mongoose disconnects (while the app is running)
mongoose.connection.on('disconnected', function () {
  setTimeout(function () {
    mongoose.connect(uri, options).catch(reconnectFailCallback); // now reconnectFailCallback _does_ get called
  }, randomDelayOfAboutASecond);
});

But indeed it works like that (which is nonsensical):

// First kill mongod instance so that Mongoose cannot connect and start the app
var dummyNoopCallback = function () {};
mongoose.connect(uri, options, dummyNoopCallback).catch(failCallback); // failCallback _does_ get called

Why is that?

It works! But will these changes break something for those who expect that application process will be finished when connection error is happened and there are no callback and promise handlers?

Example:

const mongoose = require('mongoose');
mongoose.Promise = Promise;
mongoose.connect('mongodb://127.0.0.2/test'); // no callback and promise handlers

Before these changes

/home/dmitry/example/node_modules/mongodb/lib/server.js:242
        process.nextTick(function() { throw err; })
Error: connect ECONNREFUSED 127.0.0.2:27017
    at Object.exports._errnoException (util.js:870:11)
    at exports._exceptionWithHostPort (util.js:893:20)
    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1057:14)

With these changes process is finished with 0 code

mongoose.connect('mongodb://127.0.0.2/test', function() { /* dummy function */ })
    .then(() => {
        return server.start();
    .catch(err => { // mongoose connection error will be handled here
        console.error('App starting error:', err.stack);
        process.exit(1);
    });

This is not working for me :(
It show
(node:22564) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): MongoError: failed to connect to server [127.0.0.1:27017] on first connect

I just got:

UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1):
MongoError: failed to connect to server [localhost:17551] on first connect

not using promises on mongoose 4.7.6 (latest right now), any way to avoid that Warning?
I'm using it with ExpressJS and if I use catch, it screws my logic and behaves weird as express-session is creating a MongoStore too... just messy.

When creating the initial connection towards mongodb, it could be down,
and to avoid having unhandled promise rejections (which will kill nodejs
in the future) we provide a callback to stop the error from propagating.
Also see this issue from the mongoose authors:
Automattic/mongoose#4135

Just to confirm for anyone coming later, this works as expected:

mongoose.connect('http://127.0.0.1:27017/test')
  .then(() => {
    server.start();
  .catch((err) => {
    console.log('Error on start: ' + err.stack);
    process.exit(1);
  derjp, noufalpp1988, alephreish, alersenkevich, jayjnu, kunokdev, hansfpc, bengunton, 2gnc, patrickcole, and netishix reacted with thumbs up emoji
  netishix reacted with heart emoji
    All reactions

Just to confirm for anyone coming later, this works as expected:

mongoose.connect('http://127.0.0.1:27017/test')
  .then(() => {
    server.start();
  .catch((err) => {
    console.log('Error on start: ' + err.stack);
    process.exit(1);

I'm using this approach too! But when I stop mongodb then run this code, it's not working.

// mongoose version 5.4.14
mongoose
  .connect(databaseUri, {
    promiseLibrary: bluebird,
    useNewUrlParser: true,
    useFindAndModify: false,
    useCreateIndex: true,
    // Automatically try to reconnect when it loses connection to MongoDB
    autoReconnect: true,
    // Never stop trying to reconnect
    reconnectTries: Number.MAX_VALUE,
    // Reconnect every 500ms
    reconnectInterval: 500,
    // Maintain up to 10 socket connections. If not connected,
    // return errors immediately rather than waiting for reconnect
    poolSize: 10,
    // Give up initial connection after 10 seconds
    connectTimeoutMS: 10000,
  .catch((err) => {
    console.log(err);
    process.exit(1);
  });
  • Step 1: I stopped mongodb service
  • Step 2: Connect with mongodb. At this step, no error will be printed into console, the catch block will not been reached.
  •     // Never reach to here
        console.log(err);
        process.exit(1);

    mongoose will report an error after connectTimeoutMS

    In my case, mongoose doesn't report error after connectTimeoutMS at catch block. I'm using Mongodb 3.6.5 and mongoose 5.4.14

    > mongod --version
    db version v3.6.5
    git version: a20ecd3e3a174162052ff99913bc2ca9a839d618
    OpenSSL version: OpenSSL 1.0.2p  14 Aug 2018
    allocator: system
    modules: none
    build environment:
        distarch: x86_64
        target_arch: x86_64
    needs repro script Maybe a bug, but no repro script. The issue reporter should create a script that demos the issue label Mar 18, 2019

    @nvtuan305 what operating system? Also, why do you expect Mongoose to fail to connect to the standalone - is the mongod instance down, is there no network connection, something else?

    Below script works as expected:

    const assert = require('assert');
    const mongoose = require('mongoose');
    mongoose.set('debug', true);
    run().then(() => console.log('done')).catch(error => console.error(error.stack));
    async function run() {
      console.log(mongoose.version);
      const start = Date.now();
      const opts = { useNewUrlParser: true, connectTimeoutMS: 1000 };
      await mongoose.connect('mongodb://doesntexist.com:27017/test', opts).
        catch(error => { console.log(`Caught "${error.message}" after ${Date.now() - start}`); });
    

    Output

    $ node gh-4135.js 
    5.4.14
    Caught "failed to connect to server [doesntexist.com:27017] on first connect [MongoNetworkError: connection 0 to doesntexist.com:27017 timed out]" after 1024
      can't reproduce
      Mongoose devs have been unable to reproduce this issue. Close after 14 days of inactivity.
        and removed
      needs repro script
      Maybe a bug, but no repro script. The issue reporter should create a script that demos the issue
      labels
        Apr 17, 2019
              

    Hey, it seems basic but please double check theses instructions

  • Set your IP Adress on Mongo cluster, you can even set access from everywhere
  • Be sure to handle errors, with catch for example
  • I solve it doing that