You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用MongoDB获取所有用户的全部帖子?遇500错误求助

问题:获取所有帖子接口返回500内部服务器错误

应用背景

我搭建了一个简易社交媒体应用,以下是我的Post模型代码:

const PostSchema = new mongoose.Schema(
  {
    userId: {
      type: String,
      required: true,
    },
    desc: {
      type: String,
      max: 500,
    },
    img: {
      type: String,
    },
    likes: {
      type: Array,
      default: [],
    },
  },
  { timestamps: true }
);

module.exports = mongoose.model("Post", PostSchema);

实现的接口代码

为了获取所有已存在用户的全部帖子,我在postController中编写了如下API:

const getAllPosts = async (req,res,next)=>{
    try {
      const posts = await Post.find();
      res.status(200).json(posts);
    } catch (err) {
      next(err);
      
    }
  }

路由配置

路由中间件代码如下:

//get all posts
router.get("/allposts",verifyAdmin, getAllPosts);

出现的问题

调用该接口时返回500内部服务器错误,错误截图如下:
Postman 500错误截图

我是JS新手,希望有人能帮忙解决这个问题。

内容的提问来源于stack exchange,提问作者Dragut

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.07 03:25:34