Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GET variable name contains dash creates problems for req.query for NodeJS Express?

I'm making a GET endpoint that handles this variable in Node.js using Express:

?message-timestamp=2012-08-19+20%3A38%3A23

I'm having trouble accessing it using req.query. Accessing req.query.message-timestamp throws an error ("ReferenceError: timestamp is not defined"). Clearly the dash isn't playing nice.

Any obvious way around that?

like image 902
szxk Avatar asked Mar 20 '23 01:03

szxk


1 Answers

In javascript, object values can be accessed by using either . or []
When the key contains a dash, you cannot use the . notation because the - will be interpreted as "minus". This is not related to express, it's just how javascript works.

So you should use:

req.query["message-timestamp"]
like image 99
mihai Avatar answered Apr 24 '23 17:04

mihai