Tell us what’s happening:
Hello everyone, please I need help.
when i test my code the last condition is not respected whatever i face
here is the condition :
//
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.
//
below is the part of the code that handles this condition:
ci-dessous la partie du code gérant cette condition:
/***************************************************************/
app.get("/api/users/:_id/logs", async (req, res) => {
const id = req.params._id
const {from,to, limit}=req.query
console.log( {from,to, limit})
const fromDate=new Date(from)
const toDate= new Date(to);
const limitDate=Number(limit)
try {
const logResults = await Log.findOne({
id
})
let logTable=[]
let i=0
let j=1
if(fromDate.toString()!=="Invalid Date" && toDate.toString()!=="Invalid Date" && limitDate && Boolean(logResults)){
let fromValue=fromDate.valueOf()
let toValue=toDate.valueOf()
while (i<logResults.log.length && j<=limitDate){
const logElt=new Date(logResults.log[i].date)
if (logElt.valueOf()>=fromValue && logElt.valueOf()<=toValue){
logTable.push(logResults.log[i])
j=j+1
}
i=i+1
}
console.log("first",{
_id: logResults.id,
username: logResults.username,
from:fromDate.toDateString(),
to:toDate.toDateString(),
count: limitDate,
log:logTable
})
res.json({
_id: logResults.id,
username: logResults.username,
from:fromDate.toDateString(),
to:toDate.toDateString(),
count: limitDate,
log:logTable
})
}else if(fromDate.toString()!=="Invalid Date" && toDate.toString()!=="Invalid Date" && !limit && Boolean(logResults)){
let fromValue=fromDate.valueOf()
let toValue=toDate.valueOf()
while (i<logResults.log.length){
const logElt=new Date(logResults.log[i].date)
if (logElt.valueOf()>=fromValue && logElt.valueOf()<=toValue){
logTable.push(logResults.log[i])
j=j+1
}
i=i+1
}
console.log("second",{
_id: logResults.id,
username: logResults.username,
from:fromDate.toDateString(),
to:toDate.toDateString(),
count: limitDate,
log:logTable
})
res.json({
_id: logResults.id,
username: logResults.username,
from:fromDate.toDateString(),
to:toDate.toDateString(),
count:j-1,
log:logTable
})
}else if(fromDate.toString()!=="Invalid Date" && !to && limitDate && Boolean(logResults)){
let fromValue=fromDate.valueOf()
while (i<logResults.log.length && j<=limitDate){
const logElt=new Date(logResults.log[i].date)
if (logElt.valueOf()>=fromValue ){
logTable.push(logResults.log[i])
j=j+1
}
i=i+1
}
console.log("third",{
_id: logResults.id,
username: logResults.username,
from:fromDate.toDateString(),
to:toDate.toDateString(),
count: limitDate,
log:logTable
})
res.json({
_id: logResults.id,
username: logResults.username,
from:fromDate.toDateString(),
count:j-1,
log:logTable
})
}else if(toDate.toString()!=="Invalid Date" && !from && limitDate && Boolean(logResults)){
let toValue=toDate.valueOf()
while (i<logResults.log.length && j<=limitDate){
const logElt=new Date(logResults.log[i].date)
if (logElt.valueOf()<=toValue ){
logTable.push(logResults.log[i])
j=j+1
}
i=i+1
}
console.log("fourth",{
_id: logResults.id,
username: logResults.username,
from:fromDate.toDateString(),
to:toDate.toDateString(),
count: limitDate,
log:logTable
})
res.json({
_id: logResults.id,
username: logResults.username,
to:toDate.toDateString(),
count:j-1,
log:logTable
})
}else if(fromDate.toString()!=="Invalid Date" && !to && !limit && Boolean(logResults)){
let fromValue=fromDate.valueOf()
while (i<logResults.log.length){
const logElt=new Date(logResults.log[i].date)
if (logElt.valueOf()>=fromValue ){
logTable.push(logResults.log[i])
j=j+1
}
i=i+1
}
console.log("fith",{
_id: logResults.id,
username: logResults.username,
from:fromDate.toDateString(),
to:toDate.toDateString(),
count: limitDate,
log:logTable
})
res.json({
_id: logResults.id,
username: logResults.username,
from:fromDate.toDateString(),
count:j-1,
log:logTable
})
}else if(toDate.toString()!=="Invalid Date" && !from && !limit && Boolean(logResults)){
let toValue=toDate.valueOf()
while (i<logResults.log.length){
const logElt=new Date(logResults.log[i].date)
if (logElt.valueOf()<=toValue ){
logTable.push(logResults.log[i])
j=j+1
}
i=i+1
}
console.log("sixth",{
_id: logResults.id,
username: logResults.username,
from:fromDate.toDateString(),
to:toDate.toDateString(),
count: limitDate,
log:logTable
})
res.json({
_id: logResults.id,
username: logResults.username,
to:fromDate.toDateString(),
count:j-1,
log:logTable
})
}else if(!from && !to && limitDate && Boolean(logResults)){
while (i<limitDate && i<logResults.log.length){
const logElt=new Date(logResults.log[i].date)
logTable.push(logResults.log[i])
i=i+1
}
console.log("seventh",{
_id: logResults.id,
username: logResults.username,
from:fromDate.toDateString(),
to:toDate.toDateString(),
count: limitDate,
log:logTable
})
res.json({
_id: logResults.id,
username: logResults.username,
from:fromDate.toDateString(),
count:i,
log:logTable
})
}
else {
res.json({
username: logResults.username,
count: logResults.count,
_id: logResults.id,
log: logResults.log
})
}
} catch (err) {
res.send(err)
}
})
/************************************************************/
Your project link(s)
solution: http://localhost:3000
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36
Challenge: Back End Development and APIs Projects - Exercise Tracker
Link to the challenge: