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.