I am trying to get this excercise tracker to work, but with no luck. I can’t pass add excercise request.
Request is :“I can add an exercise to any user by posting form data userId(_id), description, duration, and optionally date to /api/exercise/add. If no date supplied it will use current date. App will return the user object with the exercise fields added.”
This is my code :
//Define user schema
const userSchema = new Schema({
_id: {
type: String,
default: shortid.generate
},
username: String,
}, { versionKey: false });
//Define user model
const User = mongoose.model('user', userSchema);
const exerciseSchema = new Schema({
username: String,
description: String,
duration: Number,
_id: String,
date: Date,
}, { versionKey: false });
//Define exercise model
let Exercise = mongoose.model('exercise', exerciseSchema);
//add new exercise
app.post('/api/exercise/add', (req, res, next) => {
// console.log(req.body);
let { userId, description, duration, date } = req.body;
date = !date ? new Date() : new Date(date);
User.findById({ _id: userId }, (err, user) => {
if (err) return next(err);
new Exercise({ username: user.username, description: description, duration: duration, _id: userId, date: date }).save((err, exercise) => {
if (err) return next(err);
console.log(exercise);
return res.json(exercise);
});
});
});
Challenge: undefined
Link to the challenge: