I’m stuck on this acceptance criterion. I am not able to create a situation where the logs do not return a string, yet it fails. And, I am logging all of FCC’s attempts to the console, and nothing is coming up wrong, as far as I can tell. Any help would be appreciated.
Here is the code for the app. Never was perfect, but was nicer looking before I started trying to make this one work.
////////////////////////////////////////////////////////////////////
//additional packages
var bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({extended: false}));
app.use(bodyParser.json());
//servce static assets
app.use('/public', express.static(__dirname + '/public'));
//mongoose and db items
var mongoose = require('mongoose');
mongoose.connect(process.env.URI, { useNewUrlParser: true, useUnifiedTopology: true });
const { Schema } = mongoose;
//User
const userSchema = new Schema({
username: {type: String, required: true},
description: {type: String},
duration: {type: Number},
date: {type: String},
count: {type: Number},
log: [{
description: String,
duration: Number,
date: {type: String, required: false},
_id: false
}]
})
const User = mongoose.model('User', userSchema);
var resObj = {};
const defDate = new Date().toDateString();
var dateCheck = (input) => {
if (!input || isNaN(Date.parse(input))) {
return defDate;
} else {
return new Date(input).toDateString();
}
}
console.log(dateCheck('1111-01-01'));
//console.log(Date.parse('1111-01-01'))
app.post('/api/users', (req,res) => {
let inUser = new User({username: req.body.username});
inUser.save((err, data) => {
if (!err) {
resObj.username = data.username;
resObj._id = data.id;
res.send(resObj)
}
})
})
app.get('/api/users', (req,res) => {
User.find({}).then((users) => {
res.send(users)
});
});
app.post('/api/users/:_id/exercises', (req,res) => {
var filter = {_id: req.params._id};
var update = {
description: req.body.description,
duration: parseInt(req.body.duration),
date: dateCheck(req.body.date)
};
var options = {new: true};
User.findOneAndUpdate (filter, update, options, (err,data) => {
if (!err) {
//push to log array
data.log.push(update);
console.log('saving the date ' + typeof(update.date) + ' to the db')
data.save();
//response object with correct parameters
resObj.username = data.username;
resObj.description = data.description;
resObj.duration = data.duration;
resObj.date = data.date;
resObj._id = data.id;
res.json(resObj);
} else {
res.json({
error: err
})
}
})
})
app.get('/api/users/:_id/logs', (req,res) => {
var id = req.params._id;
var limit = req.query.limit;
var from = req.query.from;
var to = req.query.to;
if (limit) {
User.findById(id, {log: {$slice: -limit}}, (err,data) => {
res.json({
_id: id,
username: data.username,
count: data.log.length,
log: data.log
})
})
} else if (to) {
User.findById(id, (err,data) => {
res.json({
_id: id,
username: data.username,
to: to,
count: data.log.length,
log: data.log.filter(a => a.date < to)
})
})
} else if (from) {
User.findById(id, (err,data) => {
res.json({
_id: id,
username: data.username,
from: from,
count: data.log.length,
log: data.log.filter(a => a.date > from)
})
})
} else {
User.findById(id, (err,data) => {
if (!err) {
res.json({
username: data.username,
count: data.log.length,
_id: id,
log: data.log.map(a => ({description: a.description, duration: a.duration, date: a.date}))
})
}
})
}
})
///////function ends here//////////


