This is my Form
<form action="/test" method="post">
<input type="text" name="name" id="">
<input type="text" name="year" id="">
<!-- This will let me select students from my database in new club by clicking on their checkbox -->
<% students.forEach((student) => {%>
<label for="student"><%=character.name%></label>
<input type="checkbox" name="student" value="<%=student.roll%>">
<% }) %>
<button type="submit">Submit</button>
</form>
My Mongoose Schema
const studentSchema = new mongoose.Schema({
roll: String,
reg: String
})
const clubSchema = new mongoose.Schema({
name: String,
year: String,
members: [studentSchema]
})
My app.js
app.get("/test",(req,res) => {
Student.find({},(err,foundStudents) => {
if(!err){
res.render("test",{students: foundStudents})
}
})
})
app.post("/test",(req,res) => {
const newClub = new Club ({
name: req.body.name,
year: req.body.year
})
newClub.save()
})
Now my question is how do i add those students that i selected from my form in the members array of my clubSchema while registering the new club in database?
Thanks in advance and I am new to coding ,this is something i have been just playin with.If anything above is not clear to you please ask me in the comments,this is my first time asking help online so there might be some mistakes.
When you need to make a relation between collections, you should use a reference, you should not copy the entire document, because data will be duplicated.
test.ejs:
<form action="/test" method="post">
<label>Club Name:</label>
<input type="text" name="name" id="" />
<br />
<label>Year:</label>
<input type="text" name="year" id="" />
<br />
<label>Members:</label>
<br />
<% students.forEach((student, index) => {%>
<input
type="checkbox"
name="students"
id="<%=index%>"
value="<%=student._id%>"
/>
<label for="<%=index%>"><%=student.name%></label>
<% }) %>
<br />
<button type="submit">Submit</button>
<br />
<a href="/clubs">List Clubs</a>
</form>
Models.js:
const mongoose = require("mongoose");
mongoose.connect("mongodb://docker:mongopw@localhost:49153");
const studentSchema = new mongoose.Schema({
name: String,
reg: String,
});
const clubSchema = new mongoose.Schema({
name: String,
year: String,
members: [{ type: mongoose.Schema.Types.ObjectId, ref: "Student" }],
});
let Student = new mongoose.model("Student", studentSchema);
let Club = new mongoose.model("Club", clubSchema);
(async function SeedDataBase() {
let students = await Student.find();
if (students.length === 0) {
new Student({ name: "Paul", reg: "33735" }).save();
new Student({ name: "Robert", reg: "48435" }).save();
new Student({ name: "Mary", reg: "83765" }).save();
new Student({ name: "Levandowisk", reg: "09555" }).save();
}
})();
module.exports = { Student, Club };
clubs.ejs:
<a href="/test">Add Club</a>
<div>
<%clubs.forEach((club) => {%>
<label>Club Name: </label>
<label><%=club.name%></label>
<br />
<label>Year: </label>
<label><%=club.year%></label>
<br />
<label>Members:</label>
<br />
<%club.members.forEach((member) => {%>
<label>Student Name: </label>
<label><%=member.name%></label>
<br />
<% }) %>
<br />
<% }) %>
</div>
App.js:
const express = require("express");
const app = express();
const port = 3000;
const { Student, Club } = require("./Models");
app.set("view engine", "ejs");
app.use(express.urlencoded());
app.get("/test", (req, res) => {
Student.find({}, (err, students) => {
if (!err) {
res.render("test", { students });
}
});
});
app.post("/test", (req, res) => {
new Club({
name: req.body.name,
year: req.body.year,
members: req.body.students,
})
.save()
.then(() => {
res.redirect("/clubs");
});
});
app.get("/clubs", (req, res) => {
Club.find()
.populate("members")
.then((clubs, err) => {
if (!err) {
res.render("clubs", { clubs });
}
});
});
app.listen(port);