Microservices and APIs Timestamp

Hello,

I am almost done the Timestamp project. I have ticked all the criteria boxes. The only thing missing are the last two where the first one is " An empty date parameter should return the current time in a JSON object with a unix key" and the second one similar but focused on “utc”. When i run the project locally or on repl and dont put the date or anything it still comes back wrong. i am confused. Here is my code:

app.get("/api", (req,res) =>{
  const date = new Date();
  date.setMinutes(date.getMinutes()-3);
  res.json({ "unix": date.getTime(), "utc": date.toUTCString()})
});


app.get("/api/:date", function(req, res) {
  
  let  date_params = new Date(req.params.date);
  const output = "";

  if(date_params.toString() == "Invalid Date"){
    date_params = new Date(parseInt(req.params.date));
  }
  
  if(date_params.toString() !== "Invalid Date"){
    output = res.json({ "unix": date_params.getTime(), "utc": date_params.toUTCString()});
  } else{
    res.json({ error: "Invalid Date"});
  }
  return output;
});

Didn’t fully debug yet, but this is not needed imho:

  const output = "";

//...

    output = res.json({ "unix": date_params.getTime(), "utc": date_params.toUTCString()});
//...
 return output

Any of the methods there ends the request response cycle, and output is a const so it shouldn’t be reassigned.

Also I recommend using === but is not important here.

I am not sure what the error is, can you include a link please?

Thanks I removed the redundant code.
Here is the repl link:
https://boilerplate-project-timestamp.empathetic-otak.repl.co

I see. There is a previous error though, in this line:

    res.json({ "unix": date_params.getTime(), "utc": date_params.toUTCString()});

The “unix” key needs to be a number, so you need to make sure date_params.getTime() is a number. Let me know if you solve that one.

I have given you some hints here (same idea, I just refactored the code as it seems more clear):

Please, let me know if this makes sense. And then we can check your actual question.

date_params.getTime() is a number and is the only one that returns time in milliseconds. I don’t know if you were leading to something else as date_params itself takes in “new Date(req.params.date)” so that is still an integer.

1 Like

I tried handling the “blank” input of date like you did. It still does not mark the answer as right even though now we are directly addressing the lack of dates for input. Very strange. can’t understand it. It still works properly on my machine and yours now. Thanks for suggesting this though! it made me realise the error may not necessarily be on our side.

1 Like

Edit

Actually, you were correct, I made a mistake.

I’ve made a tiny change to your code (exact same structure is left), we still don’t pass, but this is how it looks:

app.get("/api",function(req, res) {
  //take what the user sent first

    const current = Date.now();
    const response = {
      "unix": parseInt(current,10), 
      "utc": (new Date()).toUTCString() 
    }
    console.log(response)
    res.json(response)
  }
)

app.get("/api/:date", function(req, res) {
  //take what the user sent first
  const date = req.params.date;
  let date_params = new Date(date);
  
 if(date_params.toString() === "Invalid Date"){
    res.json({ error: "Invalid Date"});
  }
  
  else if(date_params.toString() !== "Invalid Date"){
    res.json({ "unix": Number(date_params.getTime()), "utc": date_params.toUTCString()});
  }
  
});

So, in my forked boilerplate, that passes the first several tests (same than in yours, basically.)

Try testing in the FCC input, it won’t pass, because there are still issues to solve.

However, I remind you that the exercise specifically uses /api/:date? and you would ideally merge the first and second get into one. But I leave the fun for you.

I believe the question mark indicates express to catch the request if the parameter is not there.