Unable to run query using mongoose when stopping node js server
See original GitHub issuePrerequisites
- I have written a descriptive issue title
Mongoose version
6.3.1
Node.js version
14.17.6
MongoDB version
4.4
Operating system
Linux
Operating system version (i.e. 20.04, 11.3, 10)
20.04
Issue
I am trying to read some data from database when nodejs server getting stopped. But i did not get the data from database. Below is my code snippet.
mongoose.connect(process.env.MONGO_URI,
{
socketTimeoutMS: 60000
}
).then(() => {
console.log('Connected successfully');
})
async function gracefulShutdown() {
console.log("Closing Server");
const count = await MyModel.countDocuments();
console.log("count => ", count);
process.exit(0);
}
process.on('exit', gracefulShutdown);
Server Log :-
Connected successfully
Closing Server
count is not getting print getting undefined.
Can you please help me what is reason.
Issue Analytics
- State:
- Created a year ago
- Comments:6 (2 by maintainers)
Top Results From Across the Web
Mongoose/MongoDB in Node.JS stops returning records after ...
Mongoose /MongoDB in Node.JS stops returning records after a while · I'm facing the same issue and still can't find a way to...
Read more >Node.js ORMs: Why you shouldn't use them - LogRocket Blog
When used properly, ORM in Node.js enables you to: Avoid redundant code; Easily switch from one database to another; Query for multiple tables...
Read more >Can not create db after Node.js server restart-mongodb
Requiring the created model somewhere in the code solved my problem. var xxx=require(model/mymodel). I think Mongoose is creating the only required models.
Read more >Mongoose v6.8.2: API docs
Used for declaring paths in your schema that should be 128-bit decimal floating points. Do not use this to create a new Decimal128...
Read more >Testing Node.js + Mongoose with an in-memory database
I was worried to find any unexpected issues, but it worked perfectly! For CI pipelines I'd recommend using mongodb-memory-server-core and a ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Hi vichle,
Thanks for your reply, the code snippet you have provided is also not working. Can you please share a tested code.
This issue was closed because it has been inactive for 19 days since being marked as stale.