these two errors keep showing when submitting and this is my code
The date property of any object in the log array that is returned from
GET /api/users/:id/logs should be a string.
Use the dateString format of the Date API.
You can add from to and limit parameters to a GET /api/users/:_id/logs
request to retrieve part of the log of any user. from and to are dates in yyyy-mm-dd
format. limit is an integer of how many logs to send back. (Test timed out)
const { Schema } = mongoose;
const logSchema = new Schema({
description: {
type: String,
required: true
},
duration: {
type: Number,
required: true
},
date: {
type: String
}
})
const userSchema = new Schema({
username: String,
log:[logSchema]
});
const User = mongoose.model(“User”, userSchema);
const Log = mongoose.model(“Log”, logSchema);
const createAndSaveUser = async(name) => {
const new_user = new User({
username: name,
})
const user_data = await new_user.save()
return user_data
}
const getAllUsers = async()=>{
const users = await User.find()
return users
}
const findUser = async(id)=> {
try{
const isFounded = await User.findById(id).exec()
return isFounded
}
catch(err){
console.log(err.message)
return false
}
}
const updateUserLog = async(exercise, id)=> {
try{
const updated_log = await User.findByIdAndUpdate(id, {"$push": { “log”: exercise} }, {new: true})
}
catch(err){
console.log(err.message)
}
}
//home route
app.get(’/’, (req, res) => {
res.sendFile(__dirname + ‘/views/index.html’)
});
//create new user endpoint
app.post(’/api/users’, (req, res)=>{
const user_name = req.body.username
//check for empty user input
if(user_name===""){
res.json({
error: “please enter a username”
})
return
}
createAndSaveUser(user_name)
.then(user_saved =>{
res.json({
username: user_saved.username,
_id : user_saved._id
})
}
)
})
//show users list
app.get(’/api/users’, (req, res)=>{
//get all user from database
getAllUsers().then(users=>{
//showing only user’s id,name in the resultant array
const show_users =
users.map(user=>{
show_users.push({
_id: user._id,
username: user.username
})
})
res.json(show_users)
})
})
//create new exercise endpoint
app.post(’/api/users/:_id/exercises’, (req, res)=>{
const user_id = req.params._id
findUser(user_id)
.then(isfounded=>{
if(isfounded===false){
res.json({
error:“wrong user id”
})
return
}
isfounded.log.push({
description: req.body.description,
duration: req.body.duration,
date: req.body.date!==""?new Date(req.body.date).toDateString():new Date().toDateString()
})
isfounded.markModified(‘log’)
isfounded.save((err, result)=> {
if (!err) {
res.json({
_id: result._id,
username: result.username,
date: req.body.date!==""?new Date(req.body.date).toDateString():new Date().toDateString(),
duration: Number(req.body.duration),
description: req.body.description
})
} else {
res.status(400).send(err.message)
}
});
})
})
//show user’s logs
app.get("/api/users/:_id/logs", (req, res)=>{
const user_id = req.params._id
const query = req.query
findUser(user_id)
.then(isfounded=>{
//checking for wrong usre id
if(isfounded===false){
res.json({
error:“wrong user id”
})
return
}
//checking for request with no queries
if(Object.keys(query).length===0){
res.json({
_id: isfounded._id,
username: isfounded.username,
count : isfounded.log.length,
log : isfounded.log
})
return
}
//request with queries
let log_after_queries =
if(query.from && query.to && query.limit){
log_after_queries = isfounded.log.filter(log=>
new Date(log.date).valueOf() >= new Date(query.from).valueOf()).filter(log=>
new Date(log.date).valueOf() <= new Date(query.to).valueOf())
console.log(isfounded.log)
console.log(log_after_queries)
log_after_queries.length = parseInt(query.limit)
console.log(log_after_queries)
res.json({
_id: isfounded._id,
username: isfounded.username,
from : new Date(query.from).toDateString(),
to : new Date(query.to).toDateString(),
count : log_after_queries.length,
log : log_after_queries
})
}
})
})
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:97.0) Gecko/20100101 Firefox/97.0
Challenge: Exercise Tracker
Link to the challenge: