I am trying to figure out why my node.js application doesn't like it when I try to pass a reference to part of my POST request body as the key of an object literal. Consider the following:
//////////CODE SNIPPET
//problem section of object
"sort": [
{
req.body.sortField: {
"order": req.body.direction,
"unmapped_type": "boolean"
}
}
]
//////////REQUEST BODY
{
"fromDate": 1468213200000,
"toDate": 1468219300000,
"sortField": "#timestamp_milli",
"direction": "desc",
"columns": [
"*"
]
}
I am able to pass the direction property as a value by reference with no issues, but when I try to pass the sort property as a key I get an error when running the application:
req.body.sort: {
^
SyntaxError: Unexpected token .
My guess is that I can't pass references as keys in object literals. If this is true, why? Is there a work around for this? If it isn't true, what am I understanding incorrectly here?
Thank you for your time.
EDIT: Clarification between JSON and JavaScript object literals. Also, answered. Thank you everyone!