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!!!

有帮助吗?

解决方案

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>
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top