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 dumpreq.params
? why not use+new Date()
which gives something like1396159446837
? – 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
1 Answer
Reset to default 9In 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"]
版权声明:本文标题:javascript - GET variable name contains dash creates problems for req.query for NodeJS Express? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743902585a2558942.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论