[BUG] Back End Development and APIs Projects - Timestamp Microservice

Tell us what’s happening:
Tests of “Timestamp Microservice” excercise doesn’t works correctly, test get never passed.

Commentary:
I tried to pass the test with the next code, using replit:

app.get('/api/:date?', (req, res) => {
    
    const time = !isNaN(Number(req.params.date)) ? parseInt(req.params.date) : (req.params.date || Date.now());

    console.log(req.params.date, time);
    
    const date = new Date(time);
    const result = {};
    
    console.log(date);
    
    if(isNaN(date))
        result.error = "Invalid Date";
    else
        result.unix = parseInt(date.getTime()), result.utc = date.toUTCString();

    console.log(result);
    res.json(result);
});

But doesn’t works, so i tried the provided app exampled and it doesn’t pass either (with the same error).

I also tried: freecodecamp-back-end-development-and-apis/server.js at main · mariodmpereira/freecodecamp-back-end-development-and-apis (github.com), but that code doesn’t pass either… (same error…)

Test output:

// running tests
You should provide your own project, not the example URL.
An empty date parameter should return the current time in a JSON object with a unix key
An empty date parameter should return the current time in a JSON object with a utc key
// tests completed

Your project link(s)

solution: https://timestamp-microservice.freecodecamp.rocks

Your browser information:

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

Challenge: Back End Development and APIs Projects - Timestamp Microservice

Link to the challenge:

Do you have a link to your hosted server?

You say, “same error” but never explain what the error is or under what conditions.

The bug is in your code. Run the tests against your server with the browser console open and you’ll see errors like

frame-runner.ts:134 Error: <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Error</title>
</head>
<body>
<pre>Cannot GET /api</pre>
</body>
</html>

Your server is not handling requests to /api without a date parameter.

My code is ok, the problem is that the tests cannot be passed. There is a problem with the tests of the excercise.

I always do use of replit to do my excersises, isn’t a problem of my code or my hosting, the problem are the tests.

Even the example of page that should pass the tests does not pass the tests (https://timestamp-microservice.freecodecamp.rocks/)

What is the link you are submitting?

I tried with my own code (the same of the post entry) with replit: https://boilerplate-project-timestamp.alexandercasas.repl.co but does not pass.

I tried with the example of the excersise: https://timestamp-microservice.freecodecamp.rocks/ of course does not pass, but as my code, does not pass the same tests (this example just should produce an error, but gets 3).

Also i tried code that i found in the web, but does not pass… so the problem are the tests…

The tests that gets never passed:

// running tests
An empty date parameter should return the current time in a JSON object with a unix key
An empty date parameter should return the current time in a JSON object with a utc key
// tests completed

Here an output debug:

req.params.date: 2016-12-25
'time' passed to new Date: 2016-12-25
new Date(time): 2016-12-25T00:00:00.000Z
result output (as JSON): { unix: 1482624000000, utc: 'Sun, 25 Dec 2016 00:00:00 GMT' }
------------------------------------
req.params.date: 2016-12-25
'time' passed to new Date: 2016-12-25
new Date(time): 2016-12-25T00:00:00.000Z
result output (as JSON): { unix: 1482624000000, utc: 'Sun, 25 Dec 2016 00:00:00 GMT' }
------------------------------------
req.params.date: 1451001600000
'time' passed to new Date: 1451001600000
new Date(time): 2015-12-25T00:00:00.000Z
result output (as JSON): { unix: 1451001600000, utc: 'Fri, 25 Dec 2015 00:00:00 GMT' }
------------------------------------
req.params.date: 05 October 2011, GMT
'time' passed to new Date: 05 October 2011, GMT
new Date(time): 2011-10-05T00:00:00.000Z
result output (as JSON): { unix: 1317772800000, utc: 'Wed, 05 Oct 2011 00:00:00 GMT' }
------------------------------------
req.params.date: this-is-not-a-date
'time' passed to new Date: this-is-not-a-date
new Date(time): Invalid Date
result output (as JSON): { error: 'Invalid Date' }
------------------------------------
req.params.date: undefined
'time' passed to new Date: 1663105748610
new Date(time): 2022-09-13T21:49:08.610Z
result output (as JSON): { unix: 1663105748610, utc: 'Tue, 13 Sep 2022 21:49:08 GMT' }
------------------------------------
req.params.date: undefined
'time' passed to new Date: 1663105748924
new Date(time): 2022-09-13T21:49:08.924Z
result output (as JSON): { unix: 1663105748924, utc: 'Tue, 13 Sep 2022 21:49:08 GMT' }

Here is my code again:

app.get('/api/:date?', (req, res) => {
    
    const time = !isNaN(Number(req.params.date)) ? parseInt(req.params.date) : (req.params.date || Date.now());

    console.log("req.params.date:", req.params.date);
    console.log("'time' passed to new Date:", time);
    
    const date = new Date(time);
    const result = {};
    
    console.log(`new Date(time):`, date);
    
    if(isNaN(date))
        result.error = "Invalid Date";
    else
        result.unix = parseInt(date.getTime()), result.utc = date.toUTCString();

    console.log("result output (as JSON):", result);
    console.log('------------------------------------');
    res.json(result);
});

Thanks for your help, i hope the tests can be fixed

When I submit your project, it passes all the tests.

Peek 2022-09-13 16-16

When you submit the project url, open the browser console and see if there are any errors showing.

There is no errors

I will try using other navegator

Edit:
Doesn’t work either on Firefox T-T, i donno what is happening

If the test are runing in browser, could my location be the problem? I’m from Argentina

:confused:

This is my last excercise that i need to get certificated, what should i do?
I’m stuck, i just can’t pass the tests :confused:

Hi everyone, i did fix it. Just go to your settings and turn on “set time automatically”, now the tests will work.

Hi, I am dealing with exactly the same problem, my exercise works, but when I try to pass the tests, non of them get checked, where exactly did you “set time automatically”?
Thank you!

Depends on the OS but I’m pretty sure it should be the default. On Windows you can right-click the clock and select Adjust date/time, it is the first option on the page.

You should post your code in your own thread. I wouldn’t just assume to have the same issue (you might, you might not).

Yes, this’s what I did, and it was already “on” the option :frowning:
I’m going to open my own threat, thank you!