Timestamp Microservice (System is running but It is not passing)

Hi,

I am having a difficult time passing the Timestamp Microservice. if anyone can help feel free to message me.

// your first API endpoint…

app.get("/api/timestamp/:timestamp", function (req, res) {
  let timestamp = req.params.timestamp;
  if(timestamp.match(/\d{5,}/)){
    timestamp = +timestamp;
  }
  let date = new Date(timestamp);
  if(date.toUTCString == "Invalid Date"){
    res.json({error: date.toUTCString()})
  }
  res.json({unix: date.valueOf(), utc: date.toUTCString() });
});

app.get("/api/timestamp/", (req, res) => {
  let date = new Date();
  res.json({ unix: date.valueOf(), utc: date.toUTCString() });
})


Tell us what’s happening:

Your project link(s)

solution: https://slow-plaid-monarch.glitch.me

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.128 Safari/537.36.

Challenge: Timestamp Microservice

Link to the challenge:

If you look at the errors you posted and in the browser console when you run the tests, you’ll see that all the API GETs are trying to hit /api/:date and not /api/timestamp/:date as in the examples, the old boilerplate, and your api routes. I don’t know if this is bug, typo, or update.

Your project at least passes the eyeball test with the examples.

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

I think it could be a bug. if anyone else can help me, feel free to message in this chat, i am willing to give my work out to you.

Did you try changing the routes?

Hi Jeremy, I tried changing with your routes and its not working

You have this failing test:

A request to /api/1451001600000 should return { unix: 1451001600000, utc: "Fri, 25 Dec 2015 00:00:00 GMT" }

This is the code it is hitting:

app.get('/api/:date_string',function(req, res){
  var date = req.params.date_string;
  if(!date){
    date = new Date();
    res.json({"unix": date.getTime(), "utc" : date.toUTCString()});
  }else{
    res.json({"unix": date.getTime(), "utc" : date.toUTCString()});
  }
});

When I hit that endpoint with that param, I get back an error in the console:

TypeError: date.getTime is not a function
at /app/server.js:41:28

This is telling you what is wrong. Listen to it. It is telling you that your variable date does not have a property called getTime. How is that possible? Trace through the code.

First you have one variable called “date” that is a string. Then in the if branch (which is negative logic - I would reverse that - never use negative logic if it can be avoided). Inside that if branch you reassign date to be a Date object. OK, so that branch works fine.

In the second branch, the else branch, you immediately try to treat date as a Date object, calling the getTime method. But what is date at this point? When you can answer that, you’ll see the problem.

This is what I mentioned yesterday that I didn’t like that you had that one “date” variable representing two kinds of values - that is too confusing.

After that fix, you can get a response to that endpoint, but the response is wrong. One of those branches isn’t supposed to show the current date/time but is supposed to show the time represented by the date/time passed.

When I fix that, that test passes for me.

You need to go through each of those tests to figure out why they are failing. Use your console. Use your dev tools. With out them, it’s like trying to mow a lawn with a pair of scissors - it can be done, but there is an easier way.

thank you kevin for that detail, I will take it, run it, and get back to you with my solution.

1 Like

Hi Kevin, I completely solved it.

1 Like

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