简体   繁体   中英

Error Message from MongoDB "Operation `disneys.insertOne()` buffering timed out after 10000ms""

I'm currently creating a new API with MongoDB and Express, and I'm currently having this issue "Operation disneys.insertOne() buffering timed out after 10000ms." I'm currently using route.rest to test my API.

However, I don't know what I'm currently doing wrong, could someone take a look at my Github Repository ?

This is the way that I setup my API calls:


const express = require("express");
const router = express.Router();
const Disney = require("../models/disneyCharacter");

// Getting all character

router.get("/", async (req, res) => {
  try {
    const character = await Disney.find();
    res.json(character);
  } catch (err) {
    res.status(500).json({ message: err.message });
  }
});
// Getting one Character
router.get("/:id", getCharacter, (req, res) => {
  res.json(res.character);
});

// Creating new Character
router.post("/", async (req, res) => {
  const character = new Disney({
    name: req.body.name,
    details: req.body.details,
  });
  try {
    const newCharacter = await character.save();
    res.status(201).json({ newCharacter });
  } catch (err) {
    res.status(400).json({ message: err.message });
  }
});

// Updating one character
router.patch("/:id", getCharacter, async (req, res) => {
  if (req.body.name != null) {
    res.character.name = req.body.name;
  }
  if (req.body.details != null) {
    res.character.details = req.body.details;
  }
  try {
    const updateCharacter = await res.character.save();
    res.json(updateCharacter);
  } catch (err) {
    res.status(400).json({ message: err.message });
  }
});

// Deleting one character
router.delete("/:id", getCharacter, async (req, res) => {
  try {
    await res.character.remove();
    res.json({ message: "Deleted character" });
  } catch (err) {
    res.status(500).json({ message: err.message });
  }
});

async function getCharacter(req, res, next) {
  let character;
  try {
    character = await character.findById(req.params.id);
    if (character == null) {
      return res.status(404).json({ message: "Cannot find character" });
    }
  } catch (err) {
    return res.status(500).json({ message: err.message });
  }

  res.character = character;
  next();
}

module.exports = router;


My parameters are the following:

const mongoose = require("mongoose");

const disneyCharacter = new mongoose.Schema({
  name: {
    type: String,
    required: false,
  },
  details: {
    type: String,
    required: false,
  },
  subscribeDate: {
    type: Date,
    required: true,
    default: Date.now,
  },
});

module.exports = mongoose.model("Disney", disneyCharacter);

This is my API call:


Post http://localhost:3000/disneyCharacter
Content-Type: application/json

{
    "name": "Mickey Mouse",
    "details": "First Character from Disney"
}

Please let me know if you have any other questions or concerns.

try this out How to solve Mongoose v5.11.0 model.find() error: Operation `products.find()` buffering timed out after 10000ms"

Also, your API call seem to have a problem, It should be disneyCharacters instead of disneyCharacter .

Also, probably setup a local database first instead of using process.env.DATABASE_URL .

Actually i was also getting the same error. steps i performed to solve this error are

while creating database in mongodb

  1. allow access from anywhere (ip configuration)
  2. choose the nearest server

this solved my problems:)

In my application the same error message was thrown. The difference is, that I am using MongoDB Atlas, instead of a local MongoDB.

Solution: After added "+srv" to the URL scheme is issue was gone:

const mongoose = require("mongoose");
mongoose.set('useUnifiedTopology', true);
mongoose.set('useNewUrlParser', true);
mongoose.connect("mongodb+srv://user:password@host/dbname")
.then( () => console.log("connected to DB."))
.catch( err => console.log(err));

Dependencies in package.json:

"dependencies": {
"mongoose": "^5.11.12",
}

MongoDB Version 4.2.11

The connection string is given in the MongoDB Atlas frontend: -> Data Storage -> Cluster -> connect -> Connect your application

There you can find some code snippets.

I get this error I found Solution

Operation insertMany() buffering timed out after 10000ms"

install
API Call Server File

npm i dotenv

import express from 'express';
import mongoose from 'mongoose';
import data from './data.js';
import userRouter from './routers/userRouter.js';
import dotenv from  'dotenv';
import config from './config.js';

  dotenv.config();
  const mongodburl = config.MONGODB_URL

  const app = express();
  mongoose.connect(mongodburl, {
       useNewUrlParser: true,
       useUnifiedTopology: true,
       useCreateIndex:true,
   }).then(() => console.log('Hurry Database Connected'));

Router File install express async handler

npm i express-async-handler

import express from 'express';
import expressAsyncHandler from 'express-async-handler';
import data from '../data.js';
import User from '../models/userModel.js';
             
const userRouter = express.Router();  

userRouter.get(
      '/seed', 
      expressAsyncHandler (async(req,res)=>{
     //await User.remove({});
      const createdUsers = await User.insertMany(data.users);
      res.send({ createdUsers });     
   })
 );

I faced the same error. I am using mongoDB Atlas and not the local one. What worked for me was to remove the options in the .connect method (I am using mongoose for connecting to mongodb).

Previous code (that caused the error)

mongoose.connect(
  "" + process.env.DB_URL,
  { useUnifiedTopology: true, useNewUrlParser: true, useFindAndModify: false },
  () => { console.log("Connected to DB"); }
)

Just remove the code inside { } in this method.

mongoose.connect(
  "" + process.env.DB_URL,
  { },
  () => { console.log("Connected to DB"); }
)

Try this way too: In order to solve the error i kept the code in async await function. After successfully connected the db then only we can do the CRUD operation. Nodejs follows the single thread execution ie line by line executed. if any line is waiting for any request node automatically go to the next line and execute it first. So if a line depends on a request, put in async await function.

Add ssl=true in the URL Scheme as shown in below.

mongodb+srv://:@node-rest-courses.4qhxk.mongodb.net/myFirstDatabase?retryWrites=true&w=majority& ssl=true

in my code, only have removed parameters ssl, i am working with database in digitalOcean, my code is:

    mongoose.Promise=global.Promise; 
    mongoose.connect(process.env.MONGO_URI,{
        useNewUrlParser: true,
        useUnifiedTopology: true,
        ssl: true,
        // tlsCAFile: mongoCertPath, ----> you should remove this line
        socketTimeoutMS:43200000,
    }).then(
        ()=>{ console.log('conected to digitalOcean mongoDB: db_CRM'); },
        err=>{console.log('erro to connect digitalOcean mongoDB:'+err);}

    );

The solution to this problem is with the route.rest file since this file is not doing something correctly. For the same reason, I went ahead and create a new project in Mongo DB to set up a cluster and create the database.

Also, I tested by using POSTMAN

  • Everything is working correctly now!

Remove these two:

useCreateIndex: true,
useFindModify: false,

and put this:

useNewUrlParser: true,
useUnifiedTopology: true

final examples:

mongoose.connect(process.env.MONGODB_URL, {
    useNewUrlParser: true,
    useUnifiedTopology: true
}, err => {
    if(err) throw err;
    console.log('Connected to mongodb')
})

It is a connection problem with mongodb server. You can use IP address instead of domain/server name of your server in mongoose.connect(). In my case even localhost is not working so I replace 127.0.0.1 and everything works fine so I go back and change host file in my windows.

Yo tuve el mismo error Lo solucioné cambiando en Network Access de Mongoose Atlas el IP ADRE

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM