Tell us what’s happening:
Describe your issue in detail here.
Your project link(s)
solution: Glitch :・゚✧
Your browser information:
User Agent is: Mozilla/5.0 (Linux; Android 10; LM-K300) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Mobile Safari/537.36
Challenge: Timestamp Microservice
Link to the challenge:
If I log your route inputs and outputs like this:
let resObject = {}
app.get('/api/:input', (req, res)=>{
let input = req.params.input;
console.log(req.params.input);
if(input.includes('-')){
resObject['unix'] = new Date(input).getTime()
resObject['utc'] = new Date(input).toUTCString()
}else{
input = new Date(parseInt(input));
resObject['unix'] = new Date(input).getTime()
resObject['utc'] = new Date(input).toUTCString()
}
if(!resObject['unix'] || !resObject['utc']){
console.log({error: "Invalid Date"});
res.json({error: "Invalid Date"})
}
console.log(resObject);
res.json(resObject)
I get in the console after running the fCC tests
2016-12-25
{ unix: 1482624000000, utc: 'Sun, 25 Dec 2016 00:00:00 GMT' }
2016-12-25
{ unix: 1482624000000, utc: 'Sun, 25 Dec 2016 00:00:00 GMT' }
1451001600000
{ unix: 1451001600000, utc: 'Fri, 25 Dec 2015 00:00:00 GMT' }
05 October 2011
{ unix: 5, utc: 'Thu, 01 Jan 1970 00:00:00 GMT' }
this-is-not-a-date
{ error: 'Invalid Date' }
{ unix: NaN, utc: 'Invalid Date' }
Your problem starts with 05 October 2011
which is a parseable date string, but not by parseInt()
, which converts it to 5
as you can see from the output. The last 3 lines indicate another subtle error: you have to return responses. This
res.json(resObject)
will eventually cause Bad Things to Happen™. You really need
return res.json(resObject)
on every response unless you like things like Mocha barfing everywhere when you test a route that returns multiple responses.
system
Closed
February 7, 2022, 12:37pm
3
This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.