在新创建的项目中按现有项目
现在,这是我的表格
<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>
我的猫架架构。
const studentSchema = new mongoose.Schema({
roll: String,
reg: String
})
const clubSchema = new mongoose.Schema({
name: String,
year: String,
members: [studentSchema]
})
我的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()
})
现在我的问题是,我如何在俱乐部成员阵列中从表格中选择的那些学生在数据库中注册新俱乐部时?
预先感谢,我是编码的新手,这是我一直在玩的东西。如果您上面的任何内容都不清楚您在评论中问我,这是我第一次在网上询问帮助,因此可能会有一些错误。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当您需要在集合之间建立关系时,应使用参考,不应复制整个文档,因为数据将被复制。
test.ejs:
型号:
clubs.ejs:
app.js:
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:
Models.js:
clubs.ejs:
App.js: