问题
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.
回答1:
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
.
回答2:
Actually i was also getting the same error. steps i performed to solve this error are
while creating database in mongodb
- allow access from anywhere (ip configuration)
- choose the nearest server
this solved my problems :)
回答3:
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!
回答4:
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.
来源:https://stackoverflow.com/questions/65193572/error-message-from-mongodb-operation-disneys-insertone-buffering-timed-out