Tell us what’s happening:
Hey everybody !
I’m having trouble passing tests on the exercise Tracker project. They all fail (except the “not the example URL” one), even though my own tests work fine.
I get a “cannot read property ‘log’ of undefined” in the repl.it console when I launch the FCC tests, so the app fails which is why none of the tests passes I guess, even though the new users are created just fine in my mongodb, so this test at least should be validated :-/
Anyway, my guess would be either a misnamed path/property (even though I double checked each one to make sure they were the same as the ones on the model project, might have missed one, adunno), OR my outputs are different in types or in theur keys than the ones expected by FCC.
What do you think ?
Here’s my server.js code
const express = require('express')
const app = express()
const cors = require('cors')
const bodyParser = require('body-parser');
require('dotenv').config();
const mongoose = require("mongoose");
mongoose.connect(process.env.MONGO_URI, { useNewUrlParser: true, useUnifiedTopology: true });
app.use(bodyParser.urlencoded({extended: false}))
app.use(cors())
app.use(express.static('public'))
app.get('/', (req, res) => {
res.sendFile(__dirname + '/views/index.html')
});
let userSchema = new mongoose.Schema({
name: { type: String, required: true },
count: Number,
log: []
})
let User = mongoose.model("User", userSchema);
app.post("/api/exercise/new-user", (req, res, done) => {
let username = req.body.username
let user = new User({ name: username, count: 0, log: [] })
user.save((err, data) => {
if (err) {
return err
} else {
User.findOne({name: data.name}, (err, data) => {
if (err){
err;
}
else{
res.json({ user: data.name, id: data._id })
}
})
}
})
})
app.get("/api/exercise/users", (req, res) => {
User.find({}).select({ count: 0, log: 0 }).exec((err, data) => {
let dataObj = {};
Object.assign(dataObj, data)
res.json(dataObj)
})
})
app.post("/api/exercise/add", (req, res) => {
let exeDate;
req.body.date ? exeDate = new Date(req.body.date).toDateString() : exeDate = new Date().toDateString()
let logObj = { description: req.body.description, duration: req.body.duration, date: exeDate };
User.findOne({ _id: req.body.userId }, (err, data) => {
data.log.push(logObj);
data.count++;
data.save((err, data) => {
if (err) {
return err;
} else {
res.json({ id: data._id, username: data.name, date: logObj.date, duration: logObj.description, description: logObj.duration });
}
})
})
})
app.get("/api/exercise/log", (req, res) => {
User.findOne({ _id: req.query.userId }, (err, data) => {
let logs = data.log;
if(req.query.from) {
let from = new Date(req.query.from).getTime();
logs = logs.filter(log => new Date(log.date).getTime() >= from)
}
if(req.query.to) {
let to = new Date(req.query.to).getTime();
logs = logs.filter(log => new Date(log.date).getTime() <= to)
}
if (req.query.limit) {
logs.splice(req.query.limit, logs.length - req.query.limit)
}
res.json({ user: data.name, id: data._id, count: data.count, log: logs })
})
})
const listener = app.listen(process.env.PORT || 3000, () => {
console.log('Your app is listening on port ' + listener.address().port)
})
Thanks for your help !
Your project link(s)
solution: https://boilerplate-project-exercisetracker.frtey.repl.co
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:87.0) Gecko/20100101 Firefox/87.0
.
Challenge: Exercise Tracker
Link to the challenge: