いいね 一覧を作成中なのですがアソシエーションを組んだlikeがあるtweet情報だけを送りたいのですが
うまくいかないのでご指導お願いします。
現在のコード <template> <v-container class="mx-auto"> <v-list-item v-for="tweet in goods" :key="tweet.id" > <ForTweet :tweet ="tweet" :likes ="likes" @tweetdelete='tweet = $event' @fetchtweet='fetchTweet' @fetchlike='fetchLike' /> </v-list-item> </v-container> </template> <script> import ForTweet from "~/components/menu/tweetList/ForTweet.vue" export default { components: { ForTweet, }, data() { return { tweets: [], tweet: '', likes: [], goods: [], } }, mounted() { this.fetchTweet() this.fetchLike() }, methods: { async fetchTweet(get) { const tweets = 'http://localhost:3000/api/v1/tweets' const response = await this.$axios.get(tweets, get) this.tweets = response.data.tweets const array = this.tweets.map(value => value.like) var aryCheck = array.filter(value => { return 0 < value.length; }) for (let i = 0; i < aryCheck.length; i++) { const good = this.tweets.filter(tweet => { return tweet.like === aryCheck[i] }) this.goods.push(good); } }, async fetchLike(get) { const likes = 'http://localhost:3000/api/v1/likes' const response = await this.$axios.get(likes, get) this.likes = response.data }, } } </script>
いいねがついたTweetdataが一つだけ表示されたコード methods: { async fetchTweet(get) { const tweets = 'http://localhost:3000/api/v1/tweets' const response = await this.$axios.get(tweets, get) this.tweets = response.data.tweets const array = this.tweets.map(value => value.like) var aryCheck = array.filter(value => { return 0 < value.length; }) const good = this.tweets.filter(tweet => { return tweet.like === aryCheck[1] }) this.goods = good }, } } </script>
ここのコードの書き方が悪いと考えています const array = this.tweets.map(value => value.like) var aryCheck = array.filter(value => { return 0 < value.length; }) for (let i = 0; i < aryCheck.length; i++) { const good = this.tweets.filter(tweet => { return tweet.like === aryCheck[i] }) this.goods.push(good); console.log(good) }
console.log(good)の中身は [{…}, __ob__: Observer] 0: comment: Array(0) created_at: "2021-08-13T09:53:19.718Z" description: "ffff" id: 114 image: (...) like: Array(1) name: "ffff" picture: (...) user_id: 1 [{…}, __ob__: Observer] 0: comment: Array(2) created_at: "2021-08-13T06:07:25.292Z" description: "カメラー" id: 113 like: Array(1) name: "f" picture: (...) user_id: 8
別々のdataが入っており
送った先のthis.goodsにはこんな感じです(picture,imageは削除しました)
[ [ { "id": 114, "description": "ffff", "user_id": 1, "created_at": "2021-08-13T09:53:19.718Z", "name": "ffff", "comment": [], "like": [ { "id": 29, "user_id": 1, "tweet_id": 114, "created_at": "2021-08-13T15:27:22.784Z", "updated_at": "2021-08-13T15:27:22.784Z" } ], [ { "id": 113, "description": "カメラー", "user_id": 8, "created_at": "2021-08-13T06:07:25.292Z", "name": "f", "comment": [ { "id": 32, "user_id": 1, "tweet_id": 113, "content": "mmmmmmmm", "created_at": "2021-08-13T08:59:33.052Z", "updated_at": "2021-08-13T08:59:33.052Z" }, { "id": 33, "user_id": 1, "tweet_id": 113, "content": "jjjjjjjjjj", "created_at": "2021-08-13T08:59:41.069Z", "updated_at": "2021-08-13T08:59:41.069Z" } ], "like": [ { "id": 28, "user_id": 1, "tweet_id": 113, "created_at": "2021-08-13T09:40:11.865Z", "updated_at": "2021-08-13T09:40:11.865Z" } ] } ] ]
足りない情報あれば即ご返信いたしますのでお力をお貸しください
よろしくお願いします
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。