问题描述
我目前正在使用MongoDB和Express创建一个新的API,并且当前遇到的问题是"Operation disneys.insertOne()缓冲在10000毫秒后超时".我目前正在使用route.rest来测试我的API.
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.
但是,我不知道我当前在做什么错,有人可以看看我的 Github存储库吗?
However, I don't know what I'm currently doing wrong, could someone take a look at my Github Repository ?
这是我设置API调用的方式:
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;
我的参数如下:
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);
这是我的API调用:
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.
推荐答案
尝试一下如何解决猫鼬v5.11.0 model.find()错误:10000毫秒后操作`products.find()`缓冲超时"
此外,您的API调用似乎有问题,应该是 disneyCharacters
,而不是 disneyCharacter
.
Also, your API call seem to have a problem, It should be disneyCharacters
instead of disneyCharacter
.
此外,可能首先要设置本地数据库,而不要使用 process.env.DATABASE_URL
.
Also, probably setup a local database first instead of using process.env.DATABASE_URL
.
这篇关于来自MongoDB的错误消息"Operation`disneys.insertOne()`缓冲在10000毫秒后超时".的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!