Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Web Technology by (47.6k points)

I'm using mongoose in a script that is not meant to run continuously, and I'm facing what seems to be a very simple issue yet I can't find an answer; simply put once I make a call to any mongoose function that sends requests to mongodb my nodejs instance never stops and I have to kill it manually with, say, Ctrl+c or Program.exit().

The code looks roughly like this:

var mongoose = require('mongoose'); 

// if my program ends after this line, it shuts down as expected, my guess is that the connection is not really done here but only on the first real request ? 

mongoose.connect('mongodb://localhost:27017/somedb'); 

// define some models 

// if I include this line for example, node never stop afterwards 

var MyModel = mongoose.model('MyModel', MySchema);

I tried adding calls to mongoose.disconnect() but no to result. Aside from that, everything works fine (finding, saving, ...).

Thanks

1 Answer

0 votes
by (106k points)

To properly close mongoose's connection once you're done you can close the connection with:-

mongoose.connection.close()

Related questions

0 votes
2 answers
asked Oct 16, 2019 in Web Technology by Sammy (47.6k points)
0 votes
0 answers
0 votes
1 answer

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...