admin管理员组

文章数量:1353228

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?

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?

Share Improve this question asked Mar 30, 2014 at 6:02 szxkszxk 1,83920 silver badges35 bronze badges 3
  • 3 does req.query['message-timestamp'] work? can't you dump req.params? why not use +new Date() which gives something like 1396159446837 ? – Gntem Commented Mar 30, 2014 at 6:06
  • Aha, didn't know about bracket syntax. Yes, using Date.now() now, but was asking just for future reference. Thanks! Edit: Also the callback in question es from a texting service API, so the time that the message was sent isn't always the current time. – szxk Commented Mar 30, 2014 at 6:07
  • Does this answer your question? How do I reference a javascript object property with a hyphen in it? – Henke - Нава́льный П с м Commented Jan 29, 2021 at 10:44
Add a ment  | 

1 Answer 1

Reset to default 9

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"]

本文标签: javascriptGET variable name contains dash creates problems for reqquery for NodeJS ExpressStack Overflow