Search code examples
javascriptnode.jsserverpugharp

harp server hyphen doesn't work in jade


I am using Harp server and the hyphen (or at least I think that is this - character's name) causes an issue when trying to access metadata in my _data.json file. I am doing this inside a index.jade template.

The code like this (different name but the point is the hyphen):

.container
  for item in public.articles.idk-wtf._data.title
    .item #{ title }

The exact same code works when there is no - and of course also in the folder name. How can i get around it without changing the names of folders since I want - in the URL's and not something else like _ and such?


Solution

  • It's still Javascript; with dot notation (.), the property name must be a valid Javascript variable name, which means no hypens, spaces, etc. If you have to use hyphens or other unallowed names, use bracket notation [] which can take any string, like so:

    for item in public.articles['idk-wtf']._data.title

    Documentation (MDN): https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Property_accessors