1

I am trying to render an alternative response to my express.js route when a date value in the parameter ends up being invalid. When I see an invalid date the value ends up being null so in my if statement in the middleware I test for a truthy value and if it is not truthy i serve up an alternative response.

What I get is the true value even though the value of the date is null. Here is an example:

api/timestamp/hello is my route.

A valid date should look like this: {"unix":1546214400000,"utc":"Mon, 31 Dec 2018 00:00:00 GMT"}

An invalid date like 'hello' should look like this {'error': 'Invalid Date'}

The code returns the correct value if the date is valid, but if the date is invalid I get {"unix":null,"utc":"Invalid Date"} instead of {'error': 'Invalid Date'}

Below is the code.

   app.get('/api/timestamp/:date', (req,res) => {
    let date = new Date(req.params.date);

    if (date === null) {        
        res.send({'error': 'Invalid Date'});             
    } else {
       let unix = date.getTime();  
       let utc = date.toUTCString();
       res.send({unix, utc});  
    }  
});

I'm relatively new to express and Node.js for that matter. Any thoughts on why the null value is not being recognized?

3 Answers3

3

Q: Wouldn't it make sense to check for a valid date BEFORE you try converting it to Unix and UTC?

app.get('/api/timestamp/:date', (req,res) => {

  let date = new Date(req.params.date);
  if (req.params.date && date instanceOf Date) {
    let unix = date.getTime();  
    let utc = date.toUTCString();
    res.send({unix, utc}); 
  } else {
    res.send({'error': 'Invalid Date'});
  }
}
paulsm4
  • 114,292
  • 17
  • 138
  • 190
  • Sure that does make sense to me. I went ahead and did that. I do need to declare the date variable outside the if statement because req.params.date would still return a true value, but if I pass it through the Date method that's when I see the null value. I'm still getting the same response even though I convert the date to unix & utc. – Joe LoPresti Jan 01 '19 at 18:29
  • OK - you can check for both "date != null" and "date is valid". I've updated the example. – paulsm4 Jan 01 '19 at 21:32
1

The way you are building your response, node thinks it's a destructuring assignment, therefore you get the strange response.

To get what you want you can make something like this:

app.get('/api/timestamp/:date?', (req,res) => {
  if(req.params.date){
    let date = new Date(req.params.date);
    let unix = date.getTime();  
    let utc = date.toUTCString();
    if(unix) return res.send({unix,utc})
    res.send({'error': 'Invalid Date'});
  } else 
    res.send({'error': 'Invalid Date'});
})
Sombriks
  • 3,370
  • 4
  • 34
  • 54
  • I didn't even think about it being read as a destructuring assignment. That would make a lot of sense. I should clarify more on this problem. If req.params.date is null then I want to use the current timestamp, if it is invalid, then I want to serve the error JSON response, if it is valid I want to serve that current timestamp. – Joe LoPresti Jan 01 '19 at 18:49
  • hi, so if it's a valid date, you want to return something like {date:1546368889275}? i'll edit my answer accordingly – Sombriks Jan 01 '19 at 18:55
  • Thank you. So if it's a valid date I want it to return {"unix":1451001600000,"utc":"Fri, 25 Dec 2015 00:00:00 GMT"}. If it's an invalid date, I want it to return {'error': 'Invalid Date'}. If there is no date in the param, I'd like it to return the current timestamp, which I can currently do by just declaring a route for /api/timestamp/. – Joe LoPresti Jan 01 '19 at 19:00
  • 1
    hi, fixed the answer again. in resume you check if uni is NaN, if not you can use the res.send({unix,utc}) idiom with no concerns. – Sombriks Jan 01 '19 at 19:32
  • Thanks this definitely worked! Now I need to figure out how to get it to recognize unix dates :). – Joe LoPresti Jan 02 '19 at 00:54
0

Date constructor returns date to 1 January 1970 if the parameter is null and I can't think of any case it would return null.

Which means that your first check will always be false since your are using strict equality.

Probably you better be checking if req.params.date is truthy and unix is a valid timestamp

Hope this helps