TypeError on JSON object #781
seanlabs
started this conversation in
4. General
Replies: 1 comment
-
The filter callback doesn't look correct. this.posts.filter(
(post) => post.tags.some(
(tag) => tag.name === 'New' || tag.name === 'Assigned'
)
) |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
I'm using eager loading via Laravel to pull in models and corresponding models into a single JSON object. One of the relationships is a many to many, but the relationship does not always exist. For example, I have a many to many relationship for Post and Tag and for each Post there is not always a corresponding "Tag". So, when I try and filter on the JSON object, I receive an error "TypeError: undefined is not an object (evaluating 'post.tag.name'). It's because for that particular post there is not a corresponding tag... I can't seem to figure out how to make it ignore this in the filter function.
Is there a way to check for it being undefined and ignore inside of my filter function? Below is a snippet of my function that does the filtering of the posts to either all or current.
Beta Was this translation helpful? Give feedback.
All reactions