pzuraq / macro-decorators

Decorators for getter/setter macros!
https://pzuraq.github.io/macro-decorators/
MIT License
62 stars 10 forks source link

TypeError: Cannot read property 'filter' of undefined #21

Open st-h opened 3 years ago

st-h commented 3 years ago

I am trying to define a filter that depends on a sort decorator:

  @sortBy('inviteLinks', 'created', false)
  sortedLinks;

  @filter('sortedLink', link => link.used != null)
  displayedLinks;

However, this leads to the error: TypeError: Cannot read property 'filter' of undefined

at this function:

function filter(path, fn) {
  return macro(obj => getPath(obj, path).filter(fn));
}

Shouldn't this library be checking if the underlying array is null or undefined before assuming any further method can be called on the object?

Or to put it another way: Is there a way to make this work? I have added a constructor which initialises inviteLinks with an empty array, because I was having the same issue when only using the sortBy decorator. That solved the issue, but as soon as I added the filter decorator, it came back - but this time I have no idea how to work around this, as I think I shouldn't be setting the decorated variable sortedLinks to an empty array, to work around this issue.