1

I have simple app written in node.js. I've added few new routes, and whole service accidentally stopped work. Console shows:

(node:22568) UnhandledPromiseRejectionWarning: MongooseError: Operation `users.find()` buffering timed out after 1
0000ms
(node:22568) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwi
ng inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(
).

My connect looks:

mongoose.connect(
    process.env.DB_CONNECTION,
    {useNewUrlParser:true},
    ()=>console.log('connected to DB')
);

My route:

router.get('/index', async function (req,res) {
    console.log(req.user);
    var usr = await User.find({});
         console.log(usr);

    console.log("cars "+usr);
    res.render('main', {user: req.user, cars:usr } )
});

I tried also async connection:

(async () => {
    try {
        await mongoose.connect(
    process.env.DB_CONNECTION,
    {useNewUrlParser:true},
    ()=>console.log('connected to DB')
    );}
catch (exc){console.log('conn error!')}
})()

And route with try catch (almost the same code like above, but surrounded by try catch block). And then service processing request very long time, and then gives GET /index - - ms - - or 500. I don't know what's going on.

The Trainer
  • 633
  • 1
  • 3
  • 19
  • 1
    You should definitely use a `try/catch` inside your route controller when you use `async/await` - otherwise you're not handling errors properly and it could result in a `UnhandledPromiseRejectionWarning` error. Also, do you see the `connected to DB` message logged anywhere? – goto Apr 11 '21 at 13:38
  • I used try-catch and the same is happen (I wrote it in the question), and yes I see connected to DB everytime I run app :/ – The Trainer Apr 11 '21 at 13:43
  • 1
    Then you shouldn't be seeing `UnhandledPromiseRejectionWarning` if that's when the error is coming from. There might be an issue with your [connection](https://stackoverflow.com/questions/65418250/mongooseerror-operation-featureds-find-buffering-timed-out-after-10000ms), so I would focus on that. The code you posted is fine, assuming you wrap your route handler inside a `try/catch` – goto Apr 11 '21 at 13:47
  • @goto1 Thank you, below I added my own answer. – The Trainer Apr 11 '21 at 21:07

2 Answers2

1

The following error message:

UnhandledPromiseRejectionWarning: MongooseError: Operation `users.find()` buffering timed out after 10000ms

seems to indicate a connection issue - you may know that mongoose buffers queries in the meantime a connection actually opens, but eventually times out after 10s by default.

By any chance, have you tried listening for connection errors after the initial connection happens?

https://mongoosejs.com/docs/connections.html#error-handling

IAmDranged
  • 2,890
  • 1
  • 12
  • 6
0

Users goto1 and IAmDranged shows me the direction where I should to look for an answer. I wrote async connection to db and function with error handler. I will paste it below, maybe someone will use it in future. Once again thanks "goto1" and "IAmDranged" and my anonymous mate who helped me. Enjoy.

async function foo() {
    try {
    console.log("An attempt to connect");
await mongoose.connect(
    process.env.DB_CONNECTION,
    {useNewUrlParser:true},
    ()=>console.log('connected to DB')

)}catch (err){    console.log("Error during connection: "+err);
}}
foo();
mongoose.connection.on('error', err => {
    console.log("Error after connection: "+err);
});
The Trainer
  • 633
  • 1
  • 3
  • 19