我正在尝试创建一个锻炼管理应用程序,该应用程序允许教练(userType = true)为用户创建锻炼。不幸的是,我不确定如何格式化mongodb模式以接收大量练习。我的想法是创建一个JSON对象数组,然后将其推入数据库以稍后显示给用户。问题是我不知道如何正确创建一个可以正确解析数据的表单。我决定创建几种形式,然后使用JQuery中的.serializeObject()方法将它们转换为JSON对象,然后将它们压入数组。但是我不知道如何将这种错误推到mongoDB
这是我的gym.hbs文件
<div id ="createWorkout" class="hiddenWorkout workoutCreator">
<form id="exerciseForm" class="gymFormPadding" action="/dashboard/gym" method="POST">
<div id="exerciseInput" class="form-group" style="display:flex;width:100%">
<input id="exerciseName" class="form-control" type="text" name="name" placeholder="Exercise Name" value=""/>
<input id="exerciseSets" class="form-control" type="number" name="sets" placeholder="Sets" value=""/>
<input id="exerciseReps" class="form-control" type="number" name="reps" placeholder="Reps" value=""/>
<input id="exerciseWeight" class="form-control" type="number" name="weight" placeholder="Weight" value="" />
</div>
</form>
<div id="newExerciseAdd"></div>
<input type="hidden" name="_csrf" value="{{cybersecurity}}">
<button type="button" class="btn btn-primary" id="addExerciseAdd">Add New Exercise</button>
<button id="submitWorkout" type="submit" class="btn btn-primary" name="upload">Submit Workout</button>
</div>
这是负责操作的JQuery
var counter = 1;
$(document).ready(function(){
$("#createWorkout").click(function(){
$(".hiddenWorkout").removeClass('hiddenWorkout');
$("#createWorkout").hide();
});
});
$(document).ready(function(){
var i = 0;
$("#addExerciseAdd").click(function(){
if(i<9){
$("#exerciseForm").clone().appendTo("#newExerciseAdd");
i++;
$("#newExerciseAdd #exerciseForm").attr("id","exerciseForm"+i);
$("#newExerciseAdd #exerciseInput").attr("id","exerciseInput"+i);
$("#newExerciseAdd #exerciseName").attr("id","exerciseName"+i);
$("#newExerciseAdd #exerciseSets").attr("id","exerciseSets"+i);
$("#newExerciseAdd #exerciseReps").attr("id","exerciseReps"+i);
$("#newExerciseAdd #exerciseWeight").attr("id","exerciseWeight"+i);
counter++;
}
});
});
$(document).ready(function(){
var array = [];
var object;
var exerciseNo = exerciseNo;
$("#submitWorkout").click(function(){
$("#exerciseForm").attr("id","exerciseForm0");
$("#exerciseInput").attr("id","exerciseInput0");
$("#exerciseName").attr("id","exerciseName0");
$("#exerciseSets").attr("id","exerciseSets0");
$("#exerciseReps").attr("id","exerciseReps0");
$("#exerciseWeight").attr("id","exerciseWeight0");
for(var i=0;i<counter;i++){
object=$("#exerciseForm"+i).serializeObject();
array.push(object);
}
});
console.log(array);
});
这是我的controller.js
const mongoose = require('mongoose');
const Gym = require('../models/gym');
exports.getGymPage = async (req, res) =>
{
let coachStatus = req.user.userType;
let teamID = null;
if(res.locals.team){teamID = res.locals.team._id}
let exercises = await Gym.find({teamId:teamId});
res.render('/dashboard/gym.hbs',{title: "Gym",exercises : exercises, coachStatus : coachStatus, cybersecurity: `${req.csrfToken()}`});
};
exports.addExercise = (req, res) =>
{
const exercise = new Gym({teamID:res.locals.team._id, name: req.body.exerciseName,sets:req.body.exerciseSets,reps:req.body.exerciseReps,weight:req.body.exerciseWeight,cybersecurity: `${req.csrfToken()}`}).save();
res.redirect('/dashboard/gym');
}
这是我的models.js
const mongoose = require('mongoose');
const { Schema } = mongoose;
const gymSchema = new Schema({
teamID : {
type: mongoose.Schema.ObjectId
},
exercises:[{
name :{ type:String, require: true},
sets : [],
reps : { type: Number, require: true},
weight : { type: Number,},
}]
});
module.exports = mongoose.model('Gym', gymSchema);
我推送文件的方法完全荒谬吗?我觉得自己在转圈,无所适从。有更好的方法吗?
请原谅我最可能的可怕代码,我才刚刚起步。
谢谢!
答案 0 :(得分:0)
MongoDB应该知道它是一个数据数组。您是否尝试过通过以下方式来访问练习:
const exercise = req.body.exercise[0].name;
const exerciseData = JSON.parse(exercise);
然后进行推送,您可以只使用常规array.push 我不完全了解您要做什么,但是希望对您有所帮助...