【问题标题】:How to make options (public or private) on Post? reactjs, nodejs如何在 Post 上设置选项(公共或私人)?反应,nodejs
【发布时间】:2020-07-28 15:16:42
【问题描述】:

我正在使用 MERN 创建社交网络应用。到目前为止,我所做的一件事是用户可以创建帖子,我也有追随者和追随者。我的新任务是在帖子上做出选择,使其可以是公开的或私密的。我不知道该怎么做。有没有人想法或代码示例如何做到这一点?谢谢!

这是我的帖子模型:

const PostSchema = new Schema({
    userID: {
        type: Schema.Types.ObjectId,
        ref: 'user'
    },
    content: {
        type: String,
        required: true
    },
    registration_date: {
        type: Date,
        default: Date.now
    },
    likes: [
        {
            type: Schema.Types.ObjectId,
            ref: "user"
        }
    ],
    comments: [
        {
            text: String,
            userID: {
                type: Schema.Types.ObjectId,
                ref: 'user'
            }
        }
    ]
})

这是我的用户模型:

const UserSchema = new Schema({
    first_name: {
        type: String,
        required: true
    },
    last_name: {
        type: String,
        required: true
    },
    email: {
        type: String,
        required: true,
        unique: true
    },
    password: {
        type: String,
        required: true
    },
    registration_date: {
        type: Date,
        default: Date.now
    },
    profile_image: {
        type: String,
        default: ''
    },
    user_bio: {
        type: String,
        required: false
    },
    followers: [{
        type: Schema.Types.ObjectId,
        ref: "user"
    }],
    following: [{
        type: Schema.Types.ObjectId,
        ref: "user"
    }]
})

这里是创建帖子的逻辑:

router.post('/', auth, (req, res) => {

    const newPost = new Post({
        userID: req.user._id,
        content: req.body.content
    })

    newPost
        .save()
        .then(post => {
            res.json(post)
        })
        .catch(err => console.log(err))
})

在前端,我制作了带有“公共”和“私人”选项的下拉菜单

编辑 这是我获得所有帖子的路线:

router.get('/', auth, (req, res) => {
    Post
        .find()
        .populate('userID', 'first_name last_name profile_image _id')
        .sort({ registration_date: -1 })
        .then(post => res.json(post))
        .catch(err => res.json(err))
})

【问题讨论】:

    标签: node.js reactjs mongodb mern


    【解决方案1】:

    您可以通过多种方式解决这些问题,您可以在 Post Model 中添加一个名为“visibility”的 ENUM 类型字段,该字段目前可以接受“public”或“private”,但将来可以实现例如,“受保护”,只允许关注者看到帖子。 Post可见性的默认值可以是“public”:

    好的,添加这个字段如何解决这个问题?您可以通过这种方式向 mongo db 发送验证:

    const posts = await post.find({ userID: "123456", visibility: "public" });
    
    // posts are filled with all posts of a user that visilibity are "public", so, all posts that the user maked as "private" not will be fetched
    
    const PostSchema = new Schema({
        userID: {
            type: Schema.Types.ObjectId,
            ref: 'user'
        },
        content: {
            type: String,
            required: true
        },
        registration_date: {
            type: Date,
            default: Date.now
        },
        likes: [
            {
                type: Schema.Types.ObjectId,
                ref: "user"
            }
        ],
        // ==================
        // Add this field:
        // ==================
        visibility: {
            type: String,
            enum : ["public", "private"],
            default: "public"
        },
        comments: [
            {
                text: String,
                userID: {
                    type: Schema.Types.ObjectId,
                    ref: 'user'
                }
            }
        ]
    })
    

    注意:你可能对这个当前的数据库系统有问题,因为“评论”应该是一个新的模型,主要有三个字段:userId(评论的用户的id); postId(发布目标ID);和文本(评论文本),为什么?这样你就可以用mongoose进行分页,而不需要在一个请求中获取所有的cmets(如果你Post有500个cmets,900个?)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-11
      • 2014-07-14
      • 1970-01-01
      • 2019-02-14
      • 1970-01-01
      相关资源
      最近更新 更多