Exercise Tracker Challengue Issues

Tell us what’s happening:

I’m passing the first three goals, and I also can add exercises with & without date but for some reason is not passing the test and for some reason when I ask for the logs it is not showing me the username?? Although it’s in the model and in the DB.

I also can’t seem to find a way to implement the limits of the dates with mongoose, if somebody could help me I would appreciate it.

Your code so far

//routes
//Create User
app.post(’/api/exercise/new-user’, async function (req, res) {
let username = req.body.username;
try {
username = new Username({ username });
await username.save();
res.send(username);
} catch (e) {
res.send({ error: e.message });
}
});

//Array of all the users
//Read all users
app.get(’/api/exercise/users’, async (req, res) => {
try {
let users = await Username.find({},{log:0});
res.send(users);
} catch (e) {
res.send({ error: e.message });
}
});
//Create exercise
app.post(’/api/exercise/add’, async (req, res) => {
try {
let user = await Username.findById(req.body.userId);
if (!user) {
res.status(404).send();
}
let exercise = req.body;
//If there is no date provided
if (!exercise.date) {
exercise.date = fullDate();
}else{
exercise.date = fullDate(exercise.date);
}
user.log.push(exercise)
await user.save();
res.send(user);
} catch (e) {
res.send({ error: e.message });
}
});

//Read all exercises

app.get(’/api/exercise/log?’, async (req, res) => {
let id = req.query.id;
let from = req.query.from;
let to = req.query.to;
let limit = req.query.limit;
let user = {};
try {
user = await Username.findById(id);
if (!user) {
res.status(404).send();
}
//Limiting the data by date
if(from && to){
if(limit){
user = await Username.findById(id,{log:[{date:{$gte: from, $lte: to}},{_id:0}]}).limit(limit)
}else{
user = await Username.findById(id,{log:[{date:{$gte: from, $lte: to}},{_id:0}]})
}
}else{
user = await Username.findById(id,{log:[{_id:0}]})
}
//Counting documents
// user.count = user.log.length;
res.send(user);
} catch (e) {
res.send({ error: e.message });
}
})

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.105 Safari/537.36.

Challenge: Exercise Tracker

Link to the challenge: