vadimdemedes / mongorito

🍹 MongoDB ODM for Node.js apps based on Redux
1.38k stars 90 forks source link

MongoError: Topology was destroyed #171

Closed zixinw closed 7 years ago

zixinw commented 7 years ago

Hello vadimdemeds: I was facing a Problem when I tried to connect/disconnect to db in each request. An Exception was thrown with message " MongoError: Topology was destroyed". My code may look like:

   //with Express
   app.get(async function(req,res){
           await Mongorito.connect('...where my mongodb address is ')
           const order = await Order.findOne() // without this, the problem disappered
           await Mongorito.disconnect()
           res.end()
    })

Am I using it wrong? I tried to find out what happened in your code, and I also want to know how to manage the connections pool, but I can't find anything related in your library and homepage. Now I only use Mongorito.connect one time when the server is started, and never close the connect with mongodb (never call Mongorito.disconnect). I am not sure that my solution is ready for production, what should i do, is there any better solution to this problem, could you help me out?

vadimdemedes commented 7 years ago

You shouldn't connect to mongodb on each request. Do it once when your app starts.