Hi everyone!
I’m struggling passing the 11-15 tests.
I’m guessing it’s because my exercise objects in the log contain the _id key-value pair.
The output:
What did I do wrong and how can I fix it?
Thanks in advance
My code:
const express = require('express')
const app = express()
const cors = require('cors')
const mongoose = require('mongoose')
const { Schema } = require('mongoose')
const bodyParser = require('body-parser')
require('dotenv').config()
//conect to DB
mongoose.connect(process.env.MONGO_URL)
const exersiceSchema = new Schema({
date: String,
duration: Number,
description: String
})
const userSchema = new Schema({
username: {
type: String,
required: true
},
log: [exersiceSchema]
})
const User = mongoose.model('User', userSchema)
const Exercise = mongoose.model('Exercise', exersiceSchema)
app.use(cors())
app.use(bodyParser.urlencoded({ extended: false }))
app.use(express.static('public'))
app.get('/', (req, res) => {
res.sendFile(__dirname + '/views/index.html')
});
app.route('/api/users')
// 2-3: creat a user when submmitimg a username
.post((req, res) => {
const username = req.body.username
const user = new User({ username })
user.save((err, data) => {
if (err || !data){
res.send("There's an error saving the user");
}
res.json(data);
})
})
// 4-6: get list of users
.get((req, res) => {
User.find((err, users) => {
if(!err){
res.json(users);
}
})
})
// 7-8: add exercies to a user
app.post('/api/users/:_id/exercises', (req, res) => {
const id = req.params._id;
const { description } = req.body;
const duration = parseInt(req.body.duration);
const date = req.body.date ? new Date(req.body.date) : new Date();
const newExercise = new Exercise({
date: date.toDateString(),
duration: duration,
description: description
})
User.findByIdAndUpdate(id, {$push: {log: newExercise}}, {new: true}, (err, user) => {
if(!user || err){
res.send("Cannot find user");
}
else {
newExercise.save((err, data) => {
if(err){
res.send("There's an error saving the exercise");
}
else {
res.json({
_id: id,
username: user.username,
date: newExercise['date'],
duration: newExercise['duration'],
description: newExercise['description']
});
}
})
}
})
})
// 9-10: retrieve a full exercise log of any user
app.get('/api/users/:_id/logs', (req, res) => {
//const { from, to } = req.query;
const id = req.params._id;
//const { limit } = req.query.limit ? req.query.limit : 10;
User.findById(id, (err, user) => {
if(err || !user){
res.send("There's an error retrieving the exercise log");
} else {
let count = user.log.length;
res.json({
user,
count
});
}
})
})
// checking if the connection is good
app.get('/mongo-health', (req, res) => {
res.json({status: mongoose.connection.readyState})
})
const listener = app.listen(process.env.PORT || 3000, () => {
console.log('Your app is listening on port ' + listener.address().port)
})