如何用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内部服务器错误,错误截图如下:
我是JS新手,希望有人能帮忙解决这个问题。
内容的提问来源于stack exchange,提问作者Dragut
相关产品推荐
相关产品推荐

