Project: https://boilerplate-project-exercisetracker.berkesenturk.repl.co
Hi I’ve wrote the code as follows:
let userSchema = new mongoose.Schema({
username: {type: String, required: true, unique: true}
})
let exerciseSchema = new mongoose.Schema({
userid: {type: String},
description: {type: String},
duration: {type: Number},
date: {type: Date}
})
let User = mongoose.model('User', userSchema)
let Exercise = mongoose.model('Exercise', exerciseSchema)
app.post('/api/users/', bodyParser.urlencoded({ extended: false }),(req, res) => {
console.log(req.body)
var user = new User({username: req.body['username']});
user.save()
.then(item => {
res(201).json({"username": item.username, "_id": item._id});
})
.catch(err => {
res.status(400).send("unable to save to database");
});
})
app.get('/api/users', (req, res) => {
User.find({}, function(err, users) {
var userMap = [];
users.forEach(function(user) {
userMap.push(user);
});
res.send(userMap);
});
});
app.post('/api/users/:id/exercises',
bodyParser.urlencoded({ extended: false }),
(req, res) => {
User.findById(req.body[':_id'], (err,data) => {
if(!data) {
res.send("Unknown user id")
} else {
let newExercise = new Exercise({
userid: req.body[':_id'],
description: req.body.description,
duration: req.body.duration,
date: req.body.date
})
newExercise.save()
.then(item => {
res.json({
"userid": item.userid,
"username": data.username,
"description": item.description,
"duration": item.duration,
"date": item.date = (!req.body.date) ? new Date().toDateString() :
new Date(req.body.date).toDateString()
});
})
.catch(err => {
res.status(400).send("unable to save to database");
});
}
})
})
Even though the output is correct, still test doesn’t pass. Can you see the problem what it is?