我无法将数据添加到猫鼬中的两个不同集合

I'm not able to add data to two different collections in mongoose

我是 nodejs、mongodb 和猫鼬的新手。 我正在制作一个博客,我想在其中添加一个集合到另一个集合中。这是我的app.js代码

const express = require("express");
const bodyParser = require("body-parser");
const ejs = require("ejs");
const mongoose = require("mongoose");
const { v4: uuidv4 } = require('uuid');


const app = express();

app.use(bodyParser.urlencoded({ extended: true, limit: '50mb' }))
app.use(express.static("public"))

app.set("view engine", "ejs");


mongoose.connect("mongodb://localhost:27017/posts");


const postsSchema = new mongoose.Schema({
  _id: {
    type: String,
    required: true
  },
  title: {
    type: String,
    required: true
  },
  content: [{type: mongoose.Schema.Types.ObjectId, ref: 'content'}]
  
})
const contentSchema = new mongoose.Schema({
  _id: {
    type: String,
    required: true
  },
  subHeading: String, 
  subHeadingContent: String,
  image:{
    data: Buffer, 
    contentTyle: String
  }
})
const content = mongoose.model("Content", contentSchema)
const posts = mongoose.model("Posts", postsSchema)



let postsArray = [];
let editId;
let editTitle, editContent='';
var contents;

////////////////////////////////////    GET METHODS     ////////////////////////////////////

app.get("/", function (req, res) {
  res.render("create.ejs")
})
app.get("/overview", function (req, res) {
  posts.find((err, post) => {
    if(!err) {
      res.render("posts.ejs", {posts: post});
    }
  })
})


////////////////////////////////////    POST METHODS     ////////////////////////////////////

// Write a new post
app.post("/create", function (req, res) {
  let Posts = {
    title: req.body.newTitle,
    subheading: req.body.newSubHeading,
    subheadingcontent: req.body.newSubHeadingContent
  }
  postsArray = [];
  postsArray.push(Posts)
  postsArray.forEach((post) => {
    content.create({
      _id: uuidv4(),
      subheading: post.subheading,
      subheadingcontent: post.subheadingcontent
    })
    posts.create({
      _id: uuidv4(),
      title: post.title
    })
    
  })
    
  res.redirect("/overview");
})

// check if edit or delete button is clicked and do the required
app.post("/edit", function(req, res) {
  let editType = req.body.edit;
  let deleteId = req.body.id;
  
  if(editType === "delete") {
    posts.findOne({_id: deleteId}, (err, result) => {
      if(!err) {
      posts.deleteOne({_id: deleteId}, (err) => {
        if(err) {
          console.log(err)
        } else {
          res.redirect("/overview")
        }
      })
      }
    })
  } else if (editType ==='edit') {
      editId = req.body.id;
      posts.findOne({_id: editId}, (err, result) => {
        if(!err) {
          editTitle = result.title;
          editContent = result.content;
          res.render("editposts", {id:editId, title: editTitle, content: editContent})
        }
      })
  }

})

// resave already existing post
app.post("/savepost", function(req, res) {
  let newTitle = req.body.title;
  let newContent = req.body.content;
  console.log(contents, req.body.id, req.body.title);
  posts.findOneAndUpdate({_id: req.body.id}, {title:newTitle, content:newContent}, (err) => {
    if(!err) {
      res.redirect("/overview");
    } else {
      console.log(err);
    }
  })

})

////////////////////////////////////    LISTEN METHOD     ////////////////////////////////////

let port = process.env.PORT;
if (port == null || port == "") {
  port = 3000;
}

app.listen(port, function () {
  console.log("Server started successful");
});

基本上,没有数据被添加到 content 集合中,我不知道为什么。 postsArray 数组已填充。我正在从我的 ejs 文件中取回数据。 title 正在添加到 posts 集合中 如果您需要更多信息,请告诉我。

编辑 1: 无效的路线是:

// Write a new post
    app.post("/create", function (req, res) {
      let Posts = {
        title: req.body.newTitle,
        subheading: req.body.newsubheading,
        subheadingcontent: req.body.newsubheadingcontent
      }
      postsArray = [];
      postsArray.push(Posts)
      postsArray.forEach((post) => {
        content.create({
          _id: uuidv4(),
          subheading: post.subheading,
          subheadingcontent: post.subheadingcontent
        })
        posts.create({
          _id: uuidv4(),
          title: post.title
        })
        
      })
        
      res.redirect("/overview");
    })

此处 postsArray 中的 title 被添加到 posts 集合中,但 newsubheadingnewsubheadingcontent 未被添加到 content 集合。我不知道为什么。甚至 _id 都没有被添加

好吧,我刚刚意识到如果集合名称是单数,mongoose 会将它们变成复数,我不知道,让我失望了。我现在开始工作了