允许用户使用Mongoose / NextJS / React查看所有帖子

发布于 2025-01-22 10:02:18 字数 1375 浏览 0 评论 0原文

我正在尝试创建一个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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

厌倦 2025-01-29 10:02:18

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) to getServerSideProps through the context parameter as either a query or a parameter.

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 a param like in this codesandbox example

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文