fastify-mongodb: Error Plugin did not start in time: '@fastify/mongodb'. You may have forgotten to call 'done' function or to resolve a Promise

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the bug has not already been reported

Fastify version

4.x

Plugin version

^6.0.1

Node.js version

16.0.9

Operating system

macOS

Operating system version (i.e. 20.04, 11.3, 10)

ventura 13.5.2

Description

Why does connect localhost:27017 work. but connect to mongo with the address mongodb+srv://user:pass@cluster0.m9frj.mongodb.net/ then get an error

Plugin file

  'use strict'

  const fp = require('fastify-plugin')
  const fastifyMongo = require('@fastify/mongodb')

  module.exports = fp(async function datasourcePlugin(fastify, opts, next) {

    try {
      fastify.register(fastifyMongo, {
        forceClose: true,
        connectTimeoutMS: 10000,
        url: fastify.secrets.MONGO_URL
      });
    } catch (err) {
      next(err);
    }
  }, {
    dependencies: ['application-config']
  })

Plugin did not start in time: ‘@fastify/mongodb’. You may have forgotten to call ‘done’ function or to resolve a Promise

Steps to Reproduce

  1. config plugin and run with localhost => work
  2. config with mongolab => error

Expected Behavior

Plugin did not start in time: '@fastify/mongodb'. You may have forgotten to call 'done' function or to resolve a Promise
    at Timeout._onTimeout (/Users/admin/Desktop/app/docker/service-data/node_modules/avvio/plugin.js:122:19)
    at listOnTimeout (node:internal/timers:559:17)
    at processTimers (node:internal/timers:502:7) {
  code: 'AVV_ERR_READY_TIMEOUT',
  fn: <ref *1> [AsyncFunction: fastifyMongodb] {
    default: [Circular *1],
    fastifyMongodb: [Circular *1],
    ObjectId: [Function: ObjectId] {
      getInc: [Function (anonymous)],
      generate: [Function (anonymous)],
      createPk: [Function (anonymous)],
      createFromTime: [Function (anonymous)],
      createFromHexString: [Function (anonymous)],
      isValid: [Function (anonymous)],
      fromExtendedJSON: [Function (anonymous)],
      index: 16644978
    },
    [Symbol(skip-override)]: true,
    [Symbol(fastify.display-name)]: '@fastify/mongodb',
    [Symbol(plugin-meta)]: { fastify: '4.x', name: '@fastify/mongodb' }
  }
}

About this issue

  • Original URL
  • State: closed
  • Created 9 months ago
  • Comments: 19 (7 by maintainers)

Most upvoted comments

Thank you very much. after fixing with options with pluginTimeout server it is working.

const fastify = Fastify({
  disableRequestLogging: true,
  logger: loggerOptions,
  ajv: {
    customOptions: {
      removeAdditional: 'all'
    }
  },
  connectionTimeout: 60000,
  requestTimeout: 60000,
  pluginTimeout: 90000
})

The MongoDB Altas connection consist too many steps.

  1. DNS Resolution
  2. TCP / IP handshake
  3. Authentication

Normally, a connection will be resolved within 1 second. More than 10 second will exist in a slow network.

I tried but doing the above will get an error.

So, you would need to measure how much time you need to connect to the MongoDB Altas.

Increase the pluginTimeout to 30s or longer, if a single connection required more than 30s than it is absolutely something wrong in your network stack.

You are not doing the same as this plugin, and you ignore the connection state by resolving the plugin immediately. The below one is waiting for the connection properly.

'use strict'

const fp = require('fastify-plugin')
const mongoose = require("mongoose");

module.exports = fp(function databaseConnect(fastify, opts, done) {
  mongoose.connect('mongodb+srv://demo:demo@cluster0.m9frj.mongodb.net/').then(() => {
    console.log("connect success");
    done();
  }).catch(err => {
    console.log("ERROR MONGOO CONNECT===>", err);
    done(err);
  })
}, {
  name: 'plugin-mongoose',
  dependencies: ['application-config']
})

was about to say that in your first code version you are mixin async and next, the latter version of the code should be fine.

I think it may be a firewall or something. I would suggest to debug it with:


module.exports = fp(async function databaseConnect(fastify, opts) {
try{
  await fastify.register(fastifyMongo, {
    forceClose: true,
    url: 'mongodb+srv://demo:demo@cluster0.m9frj.mongodb.net/'
  })
} catch (err) {
console.log(err)
}
}, {
  dependencies: ['application-config']
})

This indicates that the connection to the db could not be established, as the plugin never resolves.