我目前正在使用 MongoDB 和 Express 创建一个新的 API,目前遇到以下问题:
“操作 disneys.insertOne() 缓冲在 10000 毫秒后超时。”
我目前正在使用它route.rest
来测试我的 API。
但是,我不知道我目前做错了什么,有人可以看一下我的Github 存储库吗?
这是我设置 API 调用的方式:
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;
浮云间
白衣染霜花
墨色风雨
HUWWW
湖上湖
红糖糍粑
相关分类