允许用户使用Mongoose / NextJS / React查看所有帖子
我正在尝试创建一个my-posts页面,该页面由用户发表的所有帖子组成。
用户架构
const userSchema = new Mongoose.Schema({
email: { type: String},
password: { type: String},
name: { type: String},
createdAt: { type: String},
posts: [{ type: Mongoose.Schema.Types.ObjectId, ref: 'Post'}]
},{
timestamps: true,
});
帖子架构
const postSchema = new Mongoose.Schema({
name: { type: String},
category: { type: String},
userId: {type: Mongoose.Schema.Types.ObjectId, ref: "User"},
},{
timestamps: true
});
创建一个新帖子
handler.post(async(req, res) => {
await db.connect();
const newPost = new Post({
name: req.body.name,
category: req.body.category,
userId: req.body.userId
})
const userBy = await User.findById(userId)
const thePost = await newPost.save();
userBy.posts = user.posts.concat(post._id)
await user.save()
await db.disconnect();
});
export default handler;
检索“我的帖子”,
export async function getServerSideProps( {query} ) {
await db.connect();
const data = await User.findById(req.query.id).populate("vehicles").lean
await database.disconnect();
const userPosts = data.map(database.convertObj)
return {
props: {
userPosts
}
}
}
我不太确定如何将登录的当前用户传递给getServersideProps,然后查询数据库中在帖子数组中附加到该用户的所有帖子。如果有一种更好的方法来解决这个问题,请告诉我,或者您知道我目前在做错了什么,谢谢。
I am trying to create a my-posts page which will consist of all the posts made by the user.
User Schema
const userSchema = new Mongoose.Schema({
email: { type: String},
password: { type: String},
name: { type: String},
createdAt: { type: String},
posts: [{ type: Mongoose.Schema.Types.ObjectId, ref: 'Post'}]
},{
timestamps: true,
});
Posts Schema
const postSchema = new Mongoose.Schema({
name: { type: String},
category: { type: String},
userId: {type: Mongoose.Schema.Types.ObjectId, ref: "User"},
},{
timestamps: true
});
Creating a new post
handler.post(async(req, res) => {
await db.connect();
const newPost = new Post({
name: req.body.name,
category: req.body.category,
userId: req.body.userId
})
const userBy = await User.findById(userId)
const thePost = await newPost.save();
userBy.posts = user.posts.concat(post._id)
await user.save()
await db.disconnect();
});
export default handler;
Retrieve 'My Posts'
export async function getServerSideProps( {query} ) {
await db.connect();
const data = await User.findById(req.query.id).populate("vehicles").lean
await database.disconnect();
const userPosts = data.map(database.convertObj)
return {
props: {
userPosts
}
}
}
I'm not too sure how to pass the current logged in users _id to the getServerSideProps to then query the database for all the posts attached to that user in the posts array. If there is a better way to approach this please let me know or if you know what I am currently doing wrong, thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如 nextjs docs 而不是使用
getserverversideprops
。但是,如果您真的想使用SSR进行操作,则可以使用
getserversideprops
并将数据(如用户ID)传递到getserversideprops
通过< a href =“ https://nextjs.org/docs/api-reference/data-fetching/get-server-side-side-side-props#context-parameter“ rel =“ nofollow noreferrer”>上下文参数查询
或参数
。如果您想作为查询进行此操作,则可以像已经这样做的那样将param in 此代码和盒子示例
查询
作为道具,或者可以使用This is an area where you might want to use a request from the client side to get the data, as suggested by the Nextjs docs rather than using
getServerSideProps
.But if you really want to do it with SSR, you can use
getServerSideProps
and pass in data (like the userId) togetServerSideProps
through the context parameter as either aquery
or aparameter
.If you want to do it as a query, you can get
query
as a prop, as you are already doing, or you can do it using aparam
like in this codesandbox example