gpt4 book ai didi

javascript - 来自 MongoDB 的错误消息 "Operation ` disneys.insertOne( )` buffering timed out after 10000ms""

转载 作者:行者123 更新时间:2023-12-03 12:12:18 25 4
gpt4 key购买 nike

我目前正在使用 MongoDB 和 Express 创建一个新的 API,并且我目前遇到了这个问题“操作 disneys.insertOne() 缓冲在 10000 毫秒后超时。”我目前正在使用 route.rest 来测试我的 API。
但是,我不知道我目前做错了什么,有人可以看看我的Github Repository ?
这是我设置 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;


我的参数如下:
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"
}

如果您有任何其他问题或疑虑,请告诉我。

最佳答案

试试这个
How to solve Mongoose v5.11.0 model.find() error: Operation `products.find()` buffering timed out after 10000ms"
另外,你的API调用好像有问题,应该是disneyCharacters而不是 disneyCharacter .
另外,可能首先设置本地数据库而不是使用 process.env.DATABASE_URL .

关于javascript - 来自 MongoDB 的错误消息 "Operation ` disneys.insertOne( )` buffering timed out after 10000ms"",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65193572/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com