Unending Mongoose request delay

Situation:

  • Using Mongoose v4.7.6
  • Working with MongoDB v3.2.11

Currently facing issues regarding database errors in my software.

Encountering a problem where mongoose requests hang when the database is disconnected and only resume once reconnected.


This is what occurred:

  1. Software launched successfully
  2. Established connection to the database using Mongoose
  3. Interrupted the mongod process using Ctrl+C
  4. Received both the "Disconnect" and "Close" events from mongoose
  5. Initiated a find(...) request
  6. Find request stuck indefinitely

Attempted Solutions:

Experimented with implementing the bufferCommands option in my schema, which according to the documentation, should trigger an error if no connection is available. However, the issue persists.


Snippet of My Code:

   mongoose.createConnection(..., {
      server: {
        // Reconnect feature disabled from mongoose
        auto_reconnect: false,

        socketOptions: {
          // Enabling keepAlive is advisable for long running applications
          // Without it, "connection closed" errors may occur unexpectedly after some time.
          // As per mongoose documentation
          keepAlive: 1,
        },
      },
    })

Answer №1

When errors occur in the mongoose connection, they are thrown directly from the connection itself. It happens on the main server where the connection is made, and there are different ways to handle it depending on your needs.

The find query you execute is related to a specific schema which ultimately utilizes the main connection object. You will need to manage this process for yourself, and for users, you should set a timeout for their requests and respond accordingly.

Cancelling Requests Based on Timeout

This can be managed at various levels: within your server's logic, on the client-side, or through mongoose itself.

Refer to this answer for guidance on setting timeouts with mongoose, as it is not well-documented by the library:

Within your server's logic, establish a system that handles requests without a timely response, returning an appropriate message to the client.

On the client side, if a response is not received within a certain timeframe, assume an issue has occurred.

p.s. By default, there is a timeout setting for requests

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Unable to download and install npm packages

Just today, I attempted to set up redux and encountered the following error: Interestingly, this issue isn't isolated to redux. C:\FE-Proj-Templates\webpack>npm i -D redux npm WARN package.json <a href="/cdn-cgi/l/email-protection" c ...

403 You are prohibited from adding more Parents in Google Drive API for React Native

For the past 4 months, my code with react-native-google-drive-api-wrapper was functioning perfectly. However, recently I encountered an issue where I am unable to upload files to any folder in Google Drive using my React Native app. Strangely, I can still ...

Passing a variable into the highcharts function using jquery and pug is proving to be a challenge for me

I am facing an issue while trying to pass a variable into the Highcharts function using jquery and pug. This variable is essential for setting data in series. In order to tackle this problem, I have created a function called getData in my index.js file whi ...

Struggling to locate the proper documentation for the next() function

There are multiple ways in which the next() method can be utilized: next(), next('route'), next(error)... Where is the official documentation for the next() method located? I have not been able to find a comprehensive explanation of its use cas ...

Encountering a TypeError while trying to run Pythonshell on my Mac device

When I run a python script in node.js using python shell, it works perfectly on my Windows system. However, I encounter an error when trying to run the same thing on my Macbook: Error: TypeError: can't multiply sequence by non-int of type 'float ...

What steps should I take in order to ensure that NPM commands run smoothly within Eclipse?

I have a functional workflow that I'm looking to enhance. Currently, I am developing a JavaScript library and conducting smoke tests on the code by using webpack to bundle the library and save it to a file that can be included in an HTML file for test ...

Using Express.js to send a response while simultaneously executing a background task

When working with Express.js, I have a need to execute a task after sending a response. My main goal is to minimize the response time and send back the response immediately without waiting for the task results to be returned to the client. The task itself ...

Encountering the SequelizeConnectionRefusedError while using docker in conjunction with sequelize

My Docker configuration is set up as follows: web: image: ca9a385372b0 volumes: - .:/src ports: - "8000:8000" container_name: web links: - mysql mysql: image: 7666f75adb6b environment: container_name: mysql ports: - "6603: ...

A collection of collections

Alright, listen up. I've got a JSON file with an array inside another array. Here's a snippet of the JSON file: { "keys": [ { "game": "Counter-Strike: Global Offensive", "price": "5", "listofkeys" ...

Tips for launching a React Native application with a Node.js backend on an Android device?

Currently, I'm facing an issue while trying to run my React Native app on my local Android Device. The app utilizes Node.js APIs to retrieve data from a MySQL database. However, the problem arises when my Android device is unable to access the Node.js ...

Is it possible to send a variable to a mandatory file?

While working with Express, I am attempting to move my minification process to a required file: app.js: var app = express(); var minify = require("./minify.js"); Within that file, I am trying to set my template engine. minify.js: var app = express(); ...

I am currently experiencing an issue with inserting all documents from an Excel file into my database, as the last row is not getting inserted. Can anyone offer guidance on

Hi there, I am facing an issue where the last row of my Excel file is not getting inserted into the database. Despite logging every step, it seems that the end event is firing before the data event. Can someone help me fix this problem and provide ideas to ...

Having trouble connecting to the online redis database

I am currently working on establishing a connection to my online redis database. const redis = require('redis'); const client = redis.createClient({ password: '<password>', socket: { host: <host> port: <p ...

NUXT: Module node:fs not found

Encountering an error when running yarn generate in a Kubernetes container during production. The same command works fine locally and was also functioning properly in production up until last week. Error: Cannot find module 'node:fs' Require stac ...

Define the function that will be invoked by Express.js as a parameter

When working with Express, the typical way to define routes is using app.post('/path') or app.get('/path'). I am wondering if Express has a function that allows me to define routes this way: app.route('POST', '/path&apos ...

Retrieve the unique identifier using the Mongoose function

I'm currently facing a challenge with coding a method within my mongoose model that is meant to retrieve only the ID of a specific record. Below is a simplified version of my schema: var PersonaSchema = new Schema({ email: { type: String, uniq ...

Unable to launch a stand-alone parse.com server on Debian OS

Have you heard of the standalone version of parse.com service called parse server? Check it out on https://github.com/ParsePlatform/parse-server I recently installed parse server on my Debian 8.3 system and encountered some issues with starting the servic ...

Struggling with calculations in Mongoose database operations

I have a book schema using Mongoose Schema const bookSchema = new mongoose.Schema({ bookName: { type: String, minlength: 1, maxlength: 255, required: true, trim: true }, price: { type: Number, ...

Error: The function semrush.backlinks_refdomains does not exist as a valid function

Hey there! So I've been working with the SEMRUSH API and encountered an issue when trying to retrieve data using backlinks_refdomains and backlinks_refips. However, when I called the domain_rank function, it responded in JSON format without any proble ...

Passport is raising a "missing credentials" error upon return

Hello everyone! I'm currently working on a password reset form and encountering an issue. When I submit the email in my POST form, I'm seeing a frustrating "Missing credentials" error message. This is preventing me from implementing the strategy ...