Timestamp Microservice - empty date parameter not passing

This is the last test I need to pass, I’m not sure what the issue might be.

My code:

app.get("/api/:date", (req, res) => {
  let date = req.params.date
  if (!date) {
    date = new Date()
    let unix = date.toUTCString()
    let utc = date.getTime()
    res.send({unix, utc})
  } else {
    let unix = Number(date) ? +date : new Date(date).getTime()
    let utc = new Date(unix).toUTCString()
    if (unix) {
    res.send({unix, utc})
    } else {
    res.send({ error : "Invalid Date" })
  }
  }
})

Replit link boilerplate-project-timestamp - Replit
Challenge link https://www.freecodecamp.org/learn/back-end-development-and-apis/back-end-development-and-apis-projects/timestamp-microservice

You’ve used !date to try to handle an empty date parameter.
A call to /api/:date by definition has a date parameter (i.e. this call will only be made if a date parameter is supplied by the user).

So, in the absence of a date parameter, what are you left with?

You also have the values for unix and utc back-to-front inside that conditional statement.

Thank you, I changed the conditional statement to this

if (!date || date === '') {
    date = new Date()
    let unix = date.getTime() 
    let utc = date.toUTCString()
    res.send({unix, utc})
  }

Looks like I’m still not getting it. When I try to test this statement, I just leave out the date portion in the link and get Cannot GET /api/. I think I have the wrong idea about what an empty date parameter means.

If a call is made to /api/:date, it’s because the user has supplied a value after /api/ which can be collected to populate req.params.date. If no value is supplied by the user, what api call is made?

In this post the condition if (req.params.date === '' || req.params.date === undefined) apparently worked. Why is that?

I solved it by creating a separate app.get() function like so

app.get("/api/", (req, res) => {
    let date = new Date()
    let unix = date.getTime() 
    let utc = date.toUTCString()
    res.send({unix, utc})
})

But I still don’t understand why my solution didn’t work, since a similar one in the post above did.

Ah yes, if you suffix the parameter with a ? it makes it optional, so you could make your code work that way.

Basically, there are two approaches:

  1. Make the call to /api/:date?. Making the parameter optional means that you can handle a !date condition inside this GET request. If you omit the ?, this route will not be called in the event that a parameter is not supplied.
  2. Keep it as /api/:date and make a separate call to /api, to handle no parameter.

So I guess you can make your code work simply by adding that one character, now that you’ve switched your values around.

EDIT: I see you’ve gone for option 2 (before I wrote this reply), but if you comment out that /api GET request and just add a ? to your other GET request, you’ll see that you pass that way too.

Thanks, that clears it up.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.