Timestamp Microservice "empty parameter" trouble

Tell us what’s happening:
Hi everyone! I’m working on the Timestamp Microservice project and am caught on the 5th and 6th tests having to do with an “empty” parameter. So, my understanding of an empty parameter would be that the URL looks like [project_url]/api/timestamp/, and nothing more. When I use that with my current code (like this), it seems to be functioning correctly, it returns a JSON object with the UNIX and UTC formatted times, however, it’s not passing those tests. Am I not correctly understanding what it means to pass an “empty parameter”?

Your code so far
Here is the Glitch project, I’ll also post just the .get() function here:

app.get("/api/timestamp/:date_string?", function(req, res) {
  var reqString = req.params.date_string;
  var resDate;
  if (reqString == undefined) {
    resDate = new Date();
  } else {
    if (!/^\d{4}-/.test(reqString)) reqString = parseInt(reqString);
    resDate = new Date(reqString);
    // this comparision is used to see if the date is a valid date, is there another way to do this?
    if (resDate.getTime() !== resDate.getTime()) {
      res.json({ error: "Invalid Date" });
    }
  }
  res.json({ unix: resDate.valueOf(), utc: resDate.toUTCString() });
});

Thanks so much for your attention!

Your browser information:

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

Challenge: API Project: Timestamp Microservice

Link to the challenge:

1 Like

It looks like your project is working just fine, but in order for it to pass the tests, you have to use separate .get() functions for requests with parameters and requests without.

This is from the Forum Challenge Guide for this project:

Hint: 1

You will need to create the ‘/api/timestamp/’ endpoint separately from the endpoint that reads the date to be parsed from the URL. You won’t need a conditional to deal with this endpoint.

Hope it helps!

1 Like

Hey, thanks for the quick response! I don’t know why I forgot to check those hints! I’ve made the change to having two .get() calls, but am still running into the same problem. Even more confusingly, copy and pasting the answer from the challenge guide into my own code passes even fewer of the tests then my own answer. Here’s my updated code:

// timestamp endpoint with no parameter...
app.get("/api/timestamp/", function(req, res) {
  var resDate = new Date();
  res.json({ unix: resDate.valueOf(), utc: resDate.toUTCString() });
});

// normal timestamp endpoint...
app.get("/api/timestamp/:date_string?", function(req, res) {
  var reqString = req.params.date_string;
  var resDate;
  // check to see if the string is a unix timestamp (in this challenge we can just see if it contains a dash as the 5th character), and perform the conversion to an integer if necessary
  if (!/^\d{4}-/.test(reqString)) reqString = parseInt(reqString);
  resDate = new Date(reqString);
  // this comparision is used to see if the date is a valid date, is there another way to do this?
  if (resDate.getTime() !== resDate.getTime()) {
    res.json({ error: "Invalid Date" });
  }
  res.json({ unix: resDate.valueOf(), utc: resDate.toUTCString() });
});

Looking at the challenge guide I’m really having trouble figuring out where my own answer functionally differentiates, and, again, from just copy and pasting their answer it’s still not passing all the tests, any thoughts?

1 Like

The ‘no parameter’ endpoint seems to be working well, but I think you are overcomplicating the ‘normal timestamp endpoint’.
Your regular expression only tests for a dash as the 5th character, but for this challenge, your api needs to be able to handle something like [your_url]/api/timestamp/Fri, 01 Feb 2013 15:37:40 GMT or just [your_url]/api/timestamp/01 Feb 2013

1 Like

Fortunately, the Date() constructor can handle the different kinds of parameters. If you implement it like I did at first, you will probably run into a problem with the timestamp number, because you need to convert timestamp numbers to integers before passing them to Date().

Concerning how you’re checking for valid dates, I would recommend figuring out what happens when you pass an invalid string to the Date constructor, and then using that information to make your comparison.

Keep me posted on how it goes!

Oh, I didn’t realize that ISO-8601 means that those strings could be formatted any number of ways as long as the information is in the right order! However, what I’m really concerned with at this point is why (as you said) the ‘no parameter’ endpoint seems to be working, but those are the only tests on the project page that are failing. You can check out the test page and use my project URL (https://fuchsia-thin-appliance.glitch.me) to see this.

Weird. I just tried it and it passed all the tests (I didn’t save it as my solution though). Could it be what browser you’re using?

Very odd! I’m using Chrome, and just tried Microsoft Edge (it took a lot out of me to open that up) with the same results. I’ll try again on my MacBook to see if that changes things at all. Thanks very much for your help! For now I think I’m going to move ahead to the other challenges and circle back to this one.

2 Likes

Hello! Anybody can help me with this challenge. I’m also failing the same two tests although they are working fine when I’m testing them manually. It will be a great help. Please.

Even I am stuck at the same problem when no date is passed.
https://boilerplate-project-timestamp.jatinpatel136.repl.co/api/timestamp.

works fine when checked from browser but fails when I run test on fcc

I’ve got the same problem with empty query. I’ve tried a lot of things but nothing worked. Finally i’ve grab my phone and tried to pass tests throught the Chrome browser on it and tests actually passed just fine. Wierd thing, mb it’s something with connection or PC firewall policy, i don’t know. Now i think that changing PC’s or phones is good to pass those tests.

I am having the exact same problem with the last two tests. I’ve looked at my output and it seems to be the same. I have a separate get function as well. I don’t know what could be wrong.

forgive me, is there a way I can see the variables I declare? In this particular case I would like to see “reqString” but if i console.log it I cannot see anything.