Tell us what’s happening:
When comparing my API’s results from the provided example, they appear to be the same, yet all the tests relating to POST /api/users/:_id/exercises
and /api/users/:id/logs
fail.
Your project link(s)
Here’s what the whole meat of my code looks like. I don’t use an external database by design. Is that the problem ?
const users = []
const logs = []
app.route('/api/users')
.post((req, res) => {
var id = Math.floor(Math.random() * 16777215).toString(16);
while (users.find(x => x._id == id))
id = Math.floor(Math.random() * 16777215).toString(16);
var user = { username: req.body.username, _id: id}
users.push(user);
logs.push({_id: user._id, username: user.username, count: 0, log: []})
res.json(user);
}).get((req, res) => {
res.send(users)
})
app.post('/api/users/:_id/exercises', (req, res) => {
if (logs.find(x => x._id == req.body[':_id']) &&
users.find(x => x._id == req.body[':_id']))
{
var log = logs.find(x => x._id == req.body[':_id'])
var exercise = {
description: req.body.description,
duration: parseInt(req.body.duration),
date: new Date(req.body.date).toDateString() || Date().toDateString()
}
log.count++;
log.log.push(exercise);
res.json({
_id: log._id,
user: log.username,
date: exercise.date,
duration: exercise.duration,
description: exercise.description
})
}
else
res.send('wrong ID')
})
app.get('/api/users/:id/logs', (req, res) => {
var log = logs.find(x => x._id == req.params.id)
res.json(log)
})
Your browser information:
User Agent is: Mozilla/5.0 (X11; CrOS x86_64 14324.62.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.77 Safari/537.36
Challenge: Exercise Tracker
Link to the challenge: