Giter Club home page Giter Club logo

Comments (19)

Uzlopak avatar Uzlopak commented on June 3, 2024 1

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

from fastify-mongodb.

Eomm avatar Eomm commented on June 3, 2024 1

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:[email protected]/'
  })
} catch (err) {
console.log(err)
}
}, {
  dependencies: ['application-config']
})

from fastify-mongodb.

climba03003 avatar climba03003 commented on June 3, 2024 1

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:[email protected]/').then(() => {
    console.log("connect success");
    done();
  }).catch(err => {
    console.log("ERROR MONGOO CONNECT===>", err);
    done(err);
  })
}, {
  name: 'plugin-mongoose',
  dependencies: ['application-config']
})

from fastify-mongodb.

climba03003 avatar climba03003 commented on June 3, 2024 1

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.

from fastify-mongodb.

climba03003 avatar climba03003 commented on June 3, 2024 1

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.

from fastify-mongodb.

hoanghiep1x0 avatar hoanghiep1x0 commented on June 3, 2024 1

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
})

from fastify-mongodb.

hoanghiep1x0 avatar hoanghiep1x0 commented on June 3, 2024

I have configured the db on mongo lap correctly because network open is 0.0.0.0 and user + password is correct. But I don't understand why it doesn't work

from fastify-mongodb.

hoanghiep1x0 avatar hoanghiep1x0 commented on June 3, 2024

@Uzlopak But when working on localhost:27017 it runs again, can you help me? I just learned about fastify.

from fastify-mongodb.

climba03003 avatar climba03003 commented on June 3, 2024

I am closing the issue because there is no indication the problem related to fastify.
You can still comment on the issue or seek for helps on the Discord channel.

From my point of view, it clearly you have some connection error to your database, please try the same connection string with MongoDB Compass to see if your computer allowed to connect.

from fastify-mongodb.

hoanghiep1x0 avatar hoanghiep1x0 commented on June 3, 2024

I am 100% sure the error is from @fastify/mongodb

Here is the code I run perfectly fine with mongoose

const mongoose = require("mongoose");

(async () => {
    try {
        await mongoose.connect('mongodb+srv://demo:[email protected]/')
        console.log("connect success");
    } catch (err) {
        console.log(err);
    }
})()

and here is the code that runs with fastify

'use strict'

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

module.exports = fp(async function databaseConnect(fastify, opts) {
  fastify.register(fastifyMongo, {
    forceClose: true,
    url: 'mongodb+srv://demo:[email protected]/'
  })
}, {
  dependencies: ['application-config']
})

The result is the same as the error I posted

from fastify-mongodb.

hoanghiep1x0 avatar hoanghiep1x0 commented on June 3, 2024

I still get the same error.
One confusing thing is that it works with localhost:27017

while testing with mongoose new file index.js it works. If it's due to a firewall, mongoose will also not work. Because I am running on the same local machine environment.

from fastify-mongodb.

hoanghiep1x0 avatar hoanghiep1x0 commented on June 3, 2024

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

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

Run -> ERROR ===> AvvioError [Error]: Plugin did not start in time: 'databaseConnect-auto-2'. You may have forgotten to call 'done' function or to resolve a Promise
at Timeout._onTimeout. :(

from fastify-mongodb.

climba03003 avatar climba03003 commented on June 3, 2024

I suggest you to try the official package mongodb instead of mongoose.

import { MongoClient } from 'mongodb'

const client = new MongoClient('mongodb+srv://demo:[email protected]/')
await client.connect()

from fastify-mongodb.

hoanghiep1x0 avatar hoanghiep1x0 commented on June 3, 2024

Thank. I'm just testing to find the problem and fix it.

from fastify-mongodb.

hoanghiep1x0 avatar hoanghiep1x0 commented on June 3, 2024

ERROR

'use strict'

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

module.exports = fp(function datasourcePlugin(fastify, opts, done) {
  fastify.register(fastifyMongo, {
    forceClose: true,
    url: 'mongodb+srv://demo:[email protected]/'
  });
  done();
}, {
  name: 'mongodb-plugin',
  dependencies: ['application-config']
})

A plugin does not fail

'use strict'

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

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

from fastify-mongodb.

hoanghiep1x0 avatar hoanghiep1x0 commented on June 3, 2024

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:[email protected]/').then(() => {
    console.log("connect success");
    done();
  }).catch(err => {
    console.log("ERROR MONGOO CONNECT===>", err);
    done(err);
  })
}, {
  name: 'plugin-mongoose',
  dependencies: ['application-config']
})

I tried but doing the above will get an error.

from fastify-mongodb.

hoanghiep1x0 avatar hoanghiep1x0 commented on June 3, 2024

IMG ERROR

from fastify-mongodb.

hoanghiep1x0 avatar hoanghiep1x0 commented on June 3, 2024

What is network classification error and how to fix it. You can specify the way or area to learn how to fix it

from fastify-mongodb.

hoanghiep1x0 avatar hoanghiep1x0 commented on June 3, 2024

Thanks I know how to solve the problem maybe this is it. pluginTimeout

from fastify-mongodb.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.