Tell us what’s happening:
I am trying to pass the following test:
You can
POST
to/api/users/:_id/exercises
with form datadescription
,duration
, and optionallydate
. If no date is supplied, the current date will be used. The response returned will be the user object with the exercise fields added.
And I’ve come up with the following code:
let schema = new mongoose.Schema({
username: String,
descritpion: String,
duration: Number,
date:Date
})
let Users = mongoose.model("Users", schema)
let newSchema = new mongoose.Schema({
userId:String,
description:String,
duration:Number,
date: Date
})
let Exercise = mongoose.model("Exercise", newSchema)
app.post("/api/users", (req,res) => {
let user = new Users({
username: req.body.username
})
user.save((err,data) => {
if(err) return console.log(err)
console.log("User saved successfully")
})
res.json(user)
})
app.get("/api/users", (req,res) => {
Users.find({username: /\w/},(err,data) => {
if(err) return console.log(err)
res.json(data)
})
})
app.post("/api/users/:id/exercises", (req,res) => {
const userId = req.params.id
let {description,duration,date} = req.body
if(!date) {
date = new Date()
}
Users.findOne({_id:userId}, (err,data) => {
if(err) console.log(err)
let newExercise = new Exercise({
description: description,
duration: duration,
date:date
})
const username = data.username
newExercise.save((err,data) => {
if(err) return console.log(err)
res.json({
_id: userId,
description: description,
duration: duration,
date:new Date(date).toDateString(),
username: username
})
})
})
})
But despite it seems working well it doesn’t pass the test, the connection with mongo atlas is working so I don’t think that’s the problem, I can’t figure out what the problem is.
NB: I’ve posted a good part of the code but the important part is the one that begins with app.post(“/api/:id/exercises”…
Your project link(s)
solution: boilerplate-project-exercisetracker - Replit
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.114 Safari/537.36
Challenge: Exercise Tracker
Link to the challenge: