如何在mongodb的用户模式中编辑来自posts数组对象的帖子

v64noz0r  于 2021-09-23  发布在  Java
关注(0)|答案(0)|浏览(182)

我目前正在制作一个博客网站,在那里用户可以创建帐户和撰写他们的文章(目前带有标题和正文字段),而且特定用户也可以编辑他的文章。
数据库有两个模式post和user。
请注意,中的posts字段 userSchema 由特定用户创建的帖子数组,从 postSchema 只有

//--------Post Schema--------
const postSchema = new mongoose.Schema({
  title: String,
  content: String,
  account: String,
  email: String,
  authorId: String,
  timestamp: String,
  likes: Number,
});
const Post = mongoose.model("Post", postSchema);

//--------User Schema--------
const userSchema = new mongoose.Schema({
  userHandle: String,
  email: String,
  password: String,
  googleId: String,
  posts: [postSchema],
  likedPosts: [String],
});

用户可以看到他在网站上发表的所有帖子 /profile 页我想添加一个选项来编辑帖子。我可以用中文编辑这篇文章 postSchema 但是用户的posts数组中没有更新这些内容。
这就是我编辑这篇文章的方式。

//put Edit post
app.put("/:id", async (req, res) => {
  Post.findByIdAndUpdate(
    {_id : req.params.id},
    {title : req.body.postTitle,
     content : req.body.postBody
    }, function(err){
      if(err) res.json(err);
      else{
        res.redirect("/profile");
      }
    }
    );
});

我想知道如何在网上更新帖子 posts 用户架构中的数组。

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题