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

I'm new to nodejs, mongodb and mongoose. I'm making a blog in which I want to add one collection inside another. This is my app.js code

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");
});

Basically, no data is getting added to the content collection and I have no idea why. The postsArray array is populated. I'm getting data back from my ejs files. the title is getting added to the posts collection Please let me know if you need any more information.

Edit 1: The route which is not working is:

// 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");
    })

Here the title from postsArray is getting added to the posts collection but the newsubheadingand newsubheadingcontent isnt getting added to the content collection. I have no idea why. Not even the _id is getting added



Solution 1:[1]

Ok, I just realised that mongoose makes the collection names plural if they are singular, I had no idea, threw me off. I'm getting it to work now

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Curran-C