【问题标题】:how reuse a like model in nodejs如何在 nodejs 中重用类似的模型
【发布时间】:2020-10-30 06:26:13
【问题描述】:

我有 2 个模型,分别命名为“帖子”和“状态”,并希望在其中实现点赞。首先,我希望 like 能够根据其增长方式记录时间戳和其他内容等数据,这就是为什么我让“like”成为它自己的模型。

问题在于“帖子”和“状态”这两个模型各自拥有“点赞”功能。

有没有一种方法可以重用“like”模型,而不是为“posts”和“status”创建单独的“like”模型,或者您将如何亲自实现这样的东西?

下面是帖子模型

const postSchema = new mongoose.Schema({
    title: {
        type: String,
        required: true,
        trim: true
    },
    description: {
        type: String,
        required: true,
        trim: true
    },
    owner: {
        type: mongoose.Schema.Types.ObjectId,
        required: true,
        ref: 'User'
    }
}, {
    timestamps: true
})

下面是状态模型

const statusSchema = new mongoose.Schema({
    Body: {
        type: String,
        required: true,
        trim: true
    },
    owner: {
        type: mongoose.Schema.Types.ObjectId,
        required: true,
        ref: 'User'
    },
    tags: [{
        type: mongoose.Schema.Types.ObjectId,
        required: True,
        ref: 'User'
    }]
}, {
    timestamps: true
})

这是一个喜欢模型,我希望用户能够同时喜欢用户和状态的帖子,同时仍然能够根据增长和需求保留喜欢的时间和其他信息等信息

const likeSchema = new mongoose.Schema({
    user: {
        type: mongoose.Schema.Types.ObjectId,
        required: true,
        ref: 'User'
    },
    likedObject: {
        type: mongoose.Schema.Types.ObjectId,
        required: true,
        ref: 'Posts'
    }
}, {
    timestamps: true
})

有没有一种方法可以重用“喜欢”模型,而不是为“帖子”和“状态”创建单独的“喜欢”模型来捕获用户以及他们喜欢其他用户的状态和帖子的时间?

【问题讨论】:

    标签: node.js mongodb mongoose


    【解决方案1】:

    制作 Like 模型会使整个事情变得过于复杂,因此您可以在 postSchema 中拥有类似的东西。

    const postSchema = new mongoose.Schema({
        title: {
            type: String,
            required: true,
            trim: true
        },
        description: {
            type: String,
            required: true,
            trim: true
        },
        owner: {
            type: mongoose.Schema.Types.ObjectId,
            required: true,
            ref: 'User'
        },
        timeStamps:true,
        //Adding the like property in each post but setting it to a default of 0
        likes:{type:Number, default:0}
    
    
    })
    

    现在的关键是当赞按钮被砸时识别帖子

    所以你可以在客户端做的是确保当你点击喜欢按钮时你有一个帖子的 ID 到你调用的函数,然后将该 ID 传递回服务器,你可以有一个逻辑在您的服务器上像下面这样,您就可以添加类似的功能..

    用于添加类似功能的服务器逻辑

    router.post("/api/likes/:id", async (request, response) => {
      const post_id = request.params.id;
      const post = await postModel.findOne({ _id: post_id });
    
      post.likes += 1;
    
      const updateDocument = await postModel.findOneAndUpdate(
        { _id: post_id },
        post,
        {
          new: true,
        }
      );
    
      return response.status(201).json({ msg: "Liked post" });
    });
    

    所以这个想法是两个总是更新那个特定的文档

    【讨论】:

    • 对不起,但这不是我问的问题,我解释了我制作它自己的模型的原因。请仔细阅读问题,以便您了解我想要实现的目标。
    • 快速提问,你想保留每个赞的时间戳吗?还是最近的喜欢?
    • 是的,我想保持 tiestamp 加上如果它增长,我可以尝试随着时间的推移获取更多数据
    • 好的,既然你说应该提到如何解决这个问题......这是我的看法,我仍然会使用我在回答中所做的方法,但现在是类似的结构属性应该是一个对象数组,当有人在客户端点击喜欢按钮时,您所要做的就是追加时间戳对象,但是该数组的长度应该给您喜欢的总数,您也有您想要的数据,即时间戳
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-06
    • 2022-01-23
    • 1970-01-01
    • 2017-07-21
    • 2021-11-27
    相关资源
    最近更新 更多