如何解决操作 `posts.find()` 缓冲在 10000 毫秒后超时
当我在本地尝试时,它运行良好。但 Heroku 在服务器部署后出现此错误。有人可以帮忙吗?
{
"message": "Operation `posts.find()` buffering timed out after 10000ms"
}
index.js
const express = require("express");
const cors = require("cors");
const mongoose = require("mongoose");
const bodyParser = require("body-parser");
require("dotenv").config();
const app = express();
const postRouter = require("./routes/post");
const port = process.env.PORT || 5000;
app.use(cors());
app.use(bodyParser.json({ limit: "30mb",extended: true }));
app.use(bodyParser.urlencoded({ limit: "30mb",extended: true }));
app.get("/",(req,res) => {
res.send("it works");
});
//mongoose connection
const URL = process.env.CONNECTDB_URL;
mongoose.connect(URL,{
useNewUrlParser: true,useCreateIndex: true,useUnifiedTopology: true,});
const connection = mongoose.connection;
connection.once("open",() => {
console.log("MongoDB database connection established successfully");
});
//router
app.use("/posts",postRouter);
//listening
app.listen(port,() => {
console.log(`Server is running on port: ${port}`);
});
控制器 文件在这里。我正在尝试获取所有帖子。在本地我没有任何错误
const getPosts = async (req,res) => {
try {
const posts = await Post.find({});
res.status(200).json(posts);
} catch (error) {
res.status(404).json({
message: error.message,});
}
};
解决方法
我遇到了类似的问题,但意识到我没有在 Heroku 中正确设置 CONNECTDB_URL 变量的配置变量。这可能是您的问题。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。