Question

I am getting an error trying to parse a JSON structure with Node.js, Express and DoT. It appears to be processing special characters in the JSON object below...

<td>
  {{= "Overview: " + record._source.explanation.overview + " Name: " + record._source.explanation.detailed-explanation.name }}
</td>

The first part of the query works fine (no "-" character in the structure), but when I try to access record._source.explanation.detailed-explanation.name it errors out with the message below.

undefined:2
source.explanation.analysis + " " + record._source.explanation.detailed-explanation.name
                                                                    ^
ReferenceError: explanation is not defined
    at Object.eval (eval at <anonymous> (/myapp/node_modules/express-dot/node_modules/dot/doT.js:125:11), <anonymous>:2:1715)
    at /myapp/node_modules/express-dot/express-dot.js:23:30
    at fs.js:266:14
    at Object.oncomplete (fs.js:107:15)

I have tried to escape the "-" character, such as '-' etc, but with no luck. Not really an option to go back and restructure the underlying JSON to remove '-'s. Any ideas on how to get past the special character?

Thanks!!!

Was it helpful?

Solution

You need to use square bracket notation instead of dot notation when accessing properties with names that are not valid javascript identifiers.

<td>
  {{= "Overview: " + record._source.explanation.overview + " Name: " + record._source.explanation["detailed-explanation"].name }}
</td>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top