I’m making a fitness log MEN CRUD app and am trying to push data to a MongoDB subdocument using an HTML form. When adding (creating) a new workout, the user is presented with two options between ‘run’ or ‘weight training’. The button selection takes the user to a form to fill in relevant data like date, time, distance.
I’m able to create each workout, but the data is ‘null’.
Models:
const mongoose = require('mongoose');
const RunSchema = new mongoose.Schema({
date: {
type: Number,
required: true,
},
time: Number,
distance: {
type: Number,
required: true,
},
});
const WeightliftingSchema = new mongoose.Schema({
date: {
type: Number,
required: true,
},
time: Number,
exercise: {
type: String,
required: true,
},
notes: String,
});
const workoutSchema = new mongoose.Schema({
run: [RunSchema],
weightTraining: [WeightliftingSchema],
});
const userSchema = new mongoose.Schema({
username: {
type: String,
required: true,
},
password: {
type: String,
required: true,
},
workouts: [workoutSchema],
});
const User = mongoose.model('User', userSchema);
module.exports = User;
Routers:
router.get('/new', async (req, res) => {
res.render('workouts/new.ejs')
})
router.get('/new/run', async (req, res) => {
res.render('workouts/new/run.ejs')
})
router.get('/new/weightlifting', async (req, res) => {
res.render('workouts/new/weightlifting.ejs')
})
router.post('/', async (req, res) => {
try {
const currentUser = await User.findById(req.session.user._id);
currentUser.workouts.push(req.body)
await currentUser.save();
res.redirect(`/users/${currentUser._id}/workouts`)
} catch (error) {
console.log(error)
res.redirect('/')
}
})
router.post('/:workoutId', async (req, res) => {
try {
const currentUser = await User.findById(req.session.user._id);
const workout = currentUser.workouts.id(req.params.workoutId);
currentUser.workout.run.push(req.body)
res.redirect('workouts/show.ejs', { workout: workout })
} catch (error) {
console.log(error)
res.redirect('/')
}
})
HTML form:
<form action="/users/<%= user._id %>/workouts" method="POST">
<label for="date">date:</label>
<input type="number" id="date" name="date"/>
<label for="time">time:</label>
<input type="number" id="time" name="time"/>
<label for="distance">distance:</label>
<input type="number" id="distance" name="distance"/>
<button type="submit">Add Item</button>
</form>
What I’m going for:
const userSchema = new mongoose.Schema({
username: {
type: String,
required: true,
},
password: {
type: String,
required: true,
},
workouts: [
run: [
date: DATA FROM FORM
time: DATA FROM FORM
distance: DATA FROM FORM
]
weightTraining: [null]
],
});
I’ve tried a number of things including:
- changing routes
- changing the form action
flo is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.