Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 948
Hvaandres Avatar asked Dec 08 '20 05:12

Hvaandres


2 Answers

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"); }
)
like image 125
Viraj D Avatar answered Sep 17 '22 20:09

Viraj D


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 :)

like image 36
udesh10 Avatar answered Sep 17 '22 20:09

udesh10