question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Unable to run query using mongoose when stopping node js server

See original GitHub issue

Prerequisites

  • 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:closed
  • Created a year ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
zack1991commented, Sep 28, 2022

Hi vichle,

Thanks for your reply, the code snippet you have provided is also not working. Can you please share a tested code.

0reactions
github-actions[bot]commented, Nov 14, 2022

This issue was closed because it has been inactive for 19 days since being marked as stale.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found