我正在尝试获取特定用户的帖子。它给我发了空的帖子。这是我的代码。我想我已经正确地引用了所有内容,肯定有一些错误。
User model
const Schema = mongoose.Schema
const userSchema = new Schema({
username: { type: String, required: true },
email: { type: String, reuired: true },
password: { type: String, required: true },
posts:[{ type: Schema.Types.ObjectId, ref: "Posts" }]
}, { timestamps: true })
Post model
const Schema = mongoose.Schema;
const postSchema = new Schema({
title: { type: String, required: true },
content: { type: String, required: true },
user: { type: Schema.Types.ObjectId, ref: "User" },
}, { timestamps: true }
router.get("/posts/:id", usersController.getUserPosts)
getUserPosts: (req, res) => {
User.findById(req.params.id, async (err, user) => {
if (err) {
return res.status(500).json({ error: "Server error" })
} else if (!user) {
return res.status(400).json({ error: "No user" })
} else if (user) {
user = await User.populate("user", {
path: "posts",
model: "Post"
})
return res.status(200).json({ user })
}
})
}
编辑
数据库中的用户是:
/users/list
{
"users": [
{
"posts": [],
"_id": "5e66496fcaf5d6697ca3fdbc",
"username": "JimmyPage",
"email": "[email protected]",
"password": "$2b$10$mu0IcHADj5YVIT/66EEfPOxL3cvEjqDsnJUcrST8ZcatTOcQ42kn6",
"createdAt": "2020-03-09T13:49:35.834Z",
"updatedAt": "2020-03-09T13:49:35.834Z",
"__v": 0
},
{
"posts": [],
"_id": "5e66499fcaf5d6697ca3fdbe",
"username": "AxlRose",
"email": "[email protected]",
"password": "$2b$10$H3X3efz02RonlvNXaRPr2eEbflSiFK1ITFdbyT2igUGDK9gDpIJqO",
"createdAt": "2020-03-09T13:50:23.702Z",
"updatedAt": "2020-03-09T13:50:23.702Z",
"__v": 0
}
]
}
帖子看起来像这样:
/posts/list
{
"posts": [
{
"_id": "5e66498ccaf5d6697ca3fdbd",
"title": "Jimmy Page's post",
"description": "This is Jimmy Page's post",
"user": {
"posts": [],
"_id": "5e66496fcaf5d6697ca3fdbc",
"username": "JimmyPage",
"email": "[email protected]",
"password": "$2b$10$mu0IcHADj5YVIT/66EEfPOxL3cvEjqDsnJUcrST8ZcatTOcQ42kn6",
"createdAt": "2020-03-09T13:49:35.834Z",
"updatedAt": "2020-03-09T13:49:35.834Z",
"__v": 0
},
"createdAt": "2020-03-09T13:50:04.840Z",
"updatedAt": "2020-03-09T13:50:04.840Z",
"__v": 0
},
{
"_id": "5e6649b5caf5d6697ca3fdbf",
"title": "Axl Rose's Post",
"description": "This is Axl Rose's Post",
"user": {
"posts": [],
"_id": "5e66499fcaf5d6697ca3fdbe",
"username": "AxlRose",
"email": "[email protected]",
"password": "$2b$10$H3X3efz02RonlvNXaRPr2eEbflSiFK1ITFdbyT2igUGDK9gDpIJqO",
"createdAt": "2020-03-09T13:50:23.702Z",
"updatedAt": "2020-03-09T13:50:23.702Z",
"__v": 0
},
"createdAt": "2020-03-09T13:50:45.751Z",
"updatedAt": "2020-03-09T13:50:45.751Z",
"__v": 0
},
{
"_id": "5e664b7bf120ab6c0d9999c9",
"title": "Jimmy Page's second post",
"description": "This is Jimmy Page's second post\n\n",
"user": {
"posts": [],
"_id": "5e66496fcaf5d6697ca3fdbc",
"username": "JimmyPage",
"email": "[email protected]",
"password": "$2b$10$mu0IcHADj5YVIT/66EEfPOxL3cvEjqDsnJUcrST8ZcatTOcQ42kn6",
"createdAt": "2020-03-09T13:49:35.834Z",
"updatedAt": "2020-03-09T13:49:35.834Z",
"__v": 0
},
"createdAt": "2020-03-09T13:58:19.261Z",
"updatedAt": "2020-03-09T13:58:19.261Z",
"__v": 0
}
]
}
我已在问题中添加以上代码。我一直被困在这个位置,就像我试图在他的个人资料中显示用户的帖子,并打算显示所显示的所有用户帖子的全局供稿一样。
如果您能帮助我,那将很棒。我会很感激的。谢谢。
您可以使用以下方法获取用户信息及其帖子。
getUserPosts: async (req, res) => {
try {
const user = await User.findById(req.params.id).populate("posts");
if (!user) {
return res.status(400).json({ error: "No user" });
}
return res.status(200).json({ user });
} catch (err) {
return res.status(500).json({ error: "Server error" });
}
};