Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

4章のnuxt-blog-serviceで投稿をいいねするとユーザ別投稿が消える #15

Open
wand2016 opened this issue Feb 14, 2019 · 1 comment

Comments

@wand2016
Copy link
Contributor

users/<ユーザID>/postsが消える。
image

app/store/index.jsのactions

  async addLikeLogToUser({ commit }, { user, post }) {
    user.likes.push({
      created_at: moment().format(),
      user_id: user.id,
      post_id: post.id
    })
    const newUser = await this.$axios.$put(`/users/${user.id}.json`, user)
    commit('updateUser', { user: newUser })
  },
  async removeLikeLogToUser({ commit }, { user, post }) {
    user.likes = post.likes.filter(like => like.user_id !== user.id) || []
    const newUser = await this.$axios.$put(`/users/${user.id}.json`, user)
    commit('updateUser', { user: newUser })
  }

渡ってくるuserpostsメンバが含まれておらず、これをPUTしているのが原因。

  async addLikeLogToUser({ commit }, { user, post }) {
    user.likes.push({
      created_at: moment().format(),
      user_id: user.id,
      post_id: post.id
    })
-   const newUser = await this.$axios.$put(`/users/${user.id}.json`, user)
+   const newUser = await this.$axios.$patch(`/users/${user.id}.json`, user)
    commit('updateUser', { user: newUser })
  },
  async removeLikeLogToUser({ commit }, { user, post }) {
    user.likes = post.likes.filter(like => like.user_id !== user.id) || []
-   const newUser = await this.$axios.$put(`/users/${user.id}.json`, user)
+   const newUser = await this.$axios.$patch(`/users/${user.id}.json`, user)
    commit('updateUser', { user: newUser })
  }

PUTをPATCHにすると消えなくなるが、対症療法的。
postsメンバを持たないuserがここに渡ってくるのがそもそもまずい

@potato4d
Copy link
Owner

@wand2016
ご報告ありがとうございます!確認いたしますね 👀
確認後、サンプルコードの修正が必要な場合は修正後、書籍に関しては次の印刷時に修正という形にできればと思います 🙇

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants