flarum / issue-archive

0 stars 0 forks source link

CommentPost#view: Uncaught TypeError: post.likes(...).some is not a function #271

Open dsevillamartin opened 6 years ago

dsevillamartin commented 6 years ago

Bug Report

Current Behavior I clicked https://discuss.flarum.org/d/15120 from the discussion list and the error appeared.

Steps to Reproduce Cannot reproduce on a new session, only in the same tab without refreshing.

Expected Behavior No error

Screenshots image

Environment

Possible Solution Make sure post.likes() is an array before acting on it, or default to an array somehow (doesn't look like relationships let you choose a default).

Additional Context Stack trace:

Uncaught TypeError: post.likes(...).some is not a function
    at CommentPost.<anonymous> (forum-def494fc.js:2378)
    at CommentPost.object.(anonymous function) (https://discuss.flarum.org/assets/forum-def494fc.js:2167:65)
    at CommentPost.object.(anonymous function) [as actionItems] (https://discuss.flarum.org/assets/forum-def494fc.js:2167:29)
    at forum-def494fc.js:2098
    at CommentPost.view (forum-def494fc.js:2098)
    at CommentPost.render (forum-def494fc.js:1989)
    at Object.view (forum-def494fc.js:1991)
    at checkView (forum-def494fc.js:233)
    at markViews (forum-def494fc.js:240)
    at buildObject (forum-def494fc.js:244)

Points to the following:

var isLiked = app.session.user && post.likes().some(function(user) {
  return user === app.session.user
});

Here's the output received from /api/posts?filter%5Bdiscussion%5D=15120&page%5Bnear%5D=35, though I doubt it will be useful at all: https://paste.redevs.org/paste/4

luceos commented 6 years ago

Might be related to an optional include (due to being an extension) not previously included, but required on the detail page where it reads the existing stored object instead of retrieving a refreshed version from the api..

franzliedke commented 6 years ago

Initial gut reaction: always returning an array makes things easier to deal with, essentially preventing problems like these from the get-go.

Or is there some case where the difference between a relationship being empty, and it not being there is interesting or relevant?

tobyzerner commented 6 years ago

post.likes() returns null to indicate that the relationship is not loaded, otherwise there would be no way to tell the difference between that and an empty relationship (which is definitely important to be able to tell).

It seems that the likes relationship on one or more of the posts isn't being loaded. Looking at the JSON you've linked to, there are some posts that are included (rather than in the primary data - they are probably posts which have been mentioned by the main posts) which do not contain the relationship. So there's some disconnect between what posts the API is returning in the primary data vs. what posts Flarum is trying to display on the page.

dsevillamartin commented 6 years ago

@tobscure The page works just fine when trying to reproduce the error, though. Seems like it was a rare occasion of the API returning the wrong data? It did the request twice, as I tried to load the discussion twice (going back to the discussion list before trying again). Not sure what could have caused this.