The Mongoose Error occurred due to a timeout in buffering that lasted for 10,000 milliseconds

I am attempting to establish a connection to Mongodb using the Thunder client extension in Vs code.

Here is the code snippet I have implemented so far, but unfortunately, I am encountering an error that causes the application to crash.

const mongoose = require('mongoose');

const mongoURI = "mongodb+srv://****:****@cluster0.jswzjap.mongodb.net"

const connectToMongo = ()=>{
    mongoose.connect(mongoURI, ()=>{
        console.log("Connected to Mongo Successfully");
    })
}

module.exports = connectToMongo;

The above snippet represents the connection string for MongoDB.

D:\I NOTE2\inote3\backend>nodemon ./index.js
[nodemon] 3.0.2
[nodemon] to restart at any time, enter `rs`
[nodemon] watching path(s): *.*
[nodemon] watching extensions: js,mjs,cjs,json
[nodemon] starting `node ./index.js`
Example app listening on port http://localhost:3000
{ name: 'xyz', email: '<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="b5d0d8d4dcd9f5d2d8d4dcd99bd6dad8">[email protected]</a>', password: '4567' }
D:\I NOTE2\inote3\backend\node_modules\mongoose\lib\drivers\node-mongodb-native\collection.js:186
          const err = new MongooseError(message);
                      ^

MongooseError: Operation `users.insertOne()` buffering timed out after 10000ms
    at Timeout.<anonymous> (D:\I NOTE2\inote3\backend\node_modules\mongoose\lib\drivers\node-mongodb-native\collection.js:186:23)
    at listOnTimeout (node:internal/timers:569:17)
    at process.processTimers (node:internal/timers:512:7)

Node.js v18.17.1
[nodemon] app crashed - waiting for file changes before starting...

This pertains to the specific error message that I am currently facing.

Answer №1

Make a modification to your db.js file by turning it into an async function:

const mongoose = require('mongoose');

const mongoURI = "mongodb+srv://****:****@cluster0.jswzjap.mongodb.net"

const connectToMongo = async ()=>{
   try {
      // await the connection
      const con = await mongoose.connect(mongoURI);
      console.log(`Connected to mongodb: ${con.connection.host}`);
   } catch (error) {
      throw error;
   }
}

module.exports = connectToMongo;

Next, ensure to call the function within your index.js. Simply importing it with require is not sufficient:

const connectToMongo = require('./db'); 
const express = require('express');
const app = express();
const port = 3000;
app.use(express.json()) // for parsing application/json 

// Invoke the connectToMongo function
connectToMongo().then(()=> {
   console.log('Connected to db');
}).catch(e => {
   console.log('Error Connecting to db: ', e);
})
  
// Navigate through available routes 
app.use('/api/auth', require('./routes/auth'));
app.use('/api/notes',require('./routes/notes'));
app.get('/', (req,res)=>{   res.send("hoo") });
app.listen(port, () => {   
   console.log(`Example app listening on port http://localhost:${port}`) 
});

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

Styling HTML elements with CSS to create a full width underline effect

Is there a way to make the underlines/borders full width for each line in a paragraph without adding line breaks? I'm seeking suggestions on how to achieve this. Two potential solutions I've considered are using the tag or creating an image, ...

What is the importance of manually merging geometries?

After exploring the performance implications of merged geometries, I've discovered that the GPU generates draw calls for all shared geometries combined with materials (or maybe just the material count). This has led me to wonder why developers are req ...

Attempting to assign a thumbnail image to a file on Google Drive by utilizing JavaScript

I've been working on adding thumbnails to the files that I upload to Google Drive using Javascript. I'm following the instructions outlined at https://developers.google.com/drive/v3/web/file#uploading_thumbnails For my web-safe base64 image, I c ...

Instant Access to a Precise Slide

Using a slider named mySlider or SL_Slider, which is powered by the MooTools library. When on the page with the slider, there is a simple script for the href to call the appropriate slide: <a href="javascript:mySlider.numPress(3);">link</a> ...

What is the procedure for modifying the height of a button in HTML?

I wanted to add a flashing "Contact Us" button to the menu bar of my website to immediately attract people's attention when they visit. Here is the javascript code I used: <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /&g ...

Undefined output in Typescript recursion function

When working with the recursion function in TypeScript/JavaScript, I have encountered a tricky situation involving the 'this' context. Even though I attempted to use arrow functions to avoid context changes, I found that it still did not work as ...

utilizing eval() in order to retrieve a pointer to an include

I am currently developing a form widget where the form schema is fetched from an API and generated dynamically. The library I am using for this purpose (vue-form-generator) comes with built-in validators that are directly referenced within the library itse ...

Passing two variables through a Javascript URL to dynamically update a dropdown menu based on the specified values

What is the best way to send two variables to a JavaScript function in order to modify the dropdown list according to those values? $(document).ready(function() { $("#date").change(function() { $(this).after('<div id="loader">& ...

Combining Express with Consolidate and Hogan leads to an error message: "Module 'hogan' not found", as shown in the code directly from the Consolidate documentation

Whenever I attempt to send a request to the node.js server created in Coffeescript, I encounter an Error: cannot find module 'hogan'. To view the implementation, visit this link: https://gist.github.com/wmayner/306c89d7f8fbeed3f098 I have alrea ...

Transforming a Python list into a JavaScript array

Hey there, I'm in the process of creating a JavaScript array of dates to input into a jQuery datepicker addon. Here is my Django view: def autofill_featured(request): show_id = request.GET.get('show_id') show = Show.objects.get(id=s ...

Leveraging ng-switch with ng-repeat in AngularJS to dynamically apply HTML based on conditions

I have a paginated list of video thumbnails that I want to showcase in a Bootstrap fluid grid. To achieve this, I am utilizing a nested ng-repeat loop to iterate through each list for pagination purposes. Within the inner loop, another ng-repeat is used to ...

Removing an item from an array depending on a specific condition and index

I am working with an array structure that looks like this: [ {key: 4, data: {…}, qText: {…}}, {key: 4, data: {…}, qText: {…}, isVisible: false}, {key: 5, data: {…}, qText: {…}, isVisible: false}, {key: 4, data: {…}, qText: {…}, isVi ...

How to pass event data when clicking on a div using React

I'm curious about how the onClick event flows in React when clicking on a div. In my application, there's a ParentComponent that renders a ChildComponent which generates a div for each item in the props.options array. I have a couple of questio ...

Ways to retrieve the initial value and proceed with a subsequent subscription method

I have created a basic angular 7 web application with Firebase database integration. In my code, I am attempting to store the initial list in an array using the subscribe method and then display that array using console.log(). However, there seems to be a ...

Error Type Not Caught on Console

Could you please help me understand the error message below? I'm not very familiar with JavaScript. Uncaught TypeError: $(...).sss is not a function at HTMLDocument.<anonymous> (codejs.js:3) at i (jquery.min.js:2) at Object.fireWith [as resolve ...

Create a library with CSS files added, excluding any test files

As I develop a React library, I've encountered an issue where the CSS files are being ignored during the build process. In an attempt to resolve this, I included the --copy-files flag in the build script, which successful copied the CSS files but also ...

What is the best way to execute a function based on the width of the screen?

Two functions need to be called based on the screen width. Here is one attempt: var winWidth = $(window).width(); var sections = function() { $('.image').each(function(){ var $this = $(this), x = $this.find('img'). ...

The Graphql mutation successfully updated the user data, however, it mistakenly changed the password resulting in me being unable to log in after

I am relatively new to this so please bear with me. Every time I execute the mutation, I can see the changes in mlab and the user gets updated successfully. However, when I log out and try to log back in, I encounter difficulties. My suspicion is that the ...

Issue with Vue JS Components: Button function not working on second click with @click

I've been working with Vue JS and Laravel to create a modal. The first time I press the button with @click, it works fine but the next time I encounter an error. Here's my Laravel Blade code: <div class="col-span-12 mb-5" id="a ...

Retrieving information from AngularJS modal window

Seeking a solution to retrieve data FROM modal to invoking controller, as most examples only cover sending data TO the modal. What is the proper method for achieving this? Here is the form used within the modal: <form class="form-horizontal"> < ...