質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Vue.js

Vue.jsは、Webアプリケーションのインターフェースを構築するためのオープンソースJavaScriptフレームワークです。

Nuxt.js

Nuxt.jsは、ユニバーサルなSPAが開発可能なVue.jsベースのフレームワーク。UIの描画サポートに特化しており、SSRにおけるサーバーサイドとクライアントサイドのUIレンダリングなどさまざまな機能を持ちます。

JavaScript

JavaScriptは、プログラミング言語のひとつです。ネットスケープコミュニケーションズで開発されました。 開発当初はLiveScriptと呼ばれていましたが、業務提携していたサン・マイクロシステムズが開発したJavaが脚光を浴びていたことから、JavaScriptと改名されました。 動きのあるWebページを作ることを目的に開発されたもので、主要なWebブラウザのほとんどに搭載されています。

Q&A

解決済

1回答

3841閲覧

[Nuxt.js] Cannot read property 'id' of null エラーの原因と解決方法が分からない

spellbound

総合スコア190

Vue.js

Vue.jsは、Webアプリケーションのインターフェースを構築するためのオープンソースJavaScriptフレームワークです。

Nuxt.js

Nuxt.jsは、ユニバーサルなSPAが開発可能なVue.jsベースのフレームワーク。UIの描画サポートに特化しており、SSRにおけるサーバーサイドとクライアントサイドのUIレンダリングなどさまざまな機能を持ちます。

JavaScript

JavaScriptは、プログラミング言語のひとつです。ネットスケープコミュニケーションズで開発されました。 開発当初はLiveScriptと呼ばれていましたが、業務提携していたサン・マイクロシステムズが開発したJavaが脚光を浴びていたことから、JavaScriptと改名されました。 動きのあるWebページを作ることを目的に開発されたもので、主要なWebブラウザのほとんどに搭載されています。

0グッド

0クリップ

投稿2020/12/20 17:05

概要

  • Hacker News の API を叩いて記事情報を 10 件、取得。Nuxt.js + JavaScript で Vue.js の v-for を使用し、記事一覧を表示。という、アプリケーションをつくっています。
  • 詰まっていることはデータは取れているが、v-for で list レンダリングしようとすると、id にアクセスできず、エラーが出て、記事が表示できません。

取得できている情報

  • v-for で要素を一つずつ表示した結果です。データは問題なく取れています。
{ "by": "pg", "descendants": 15, "id": 1, "kids": [ 15, 234509, 487171, 454426, 454424, 454410, 82729 ], "score": 57, "time": 1160418111, "title": "Y Combinator", "type": "story", "url": "http://ycombinator.com" } { "by": "phyllis", "descendants": 0, "id": 2, "kids": [ 454411 ], "score": 16, "time": 1160418628, "title": "A Student's Guide to Startups", "type": "story", "url": "http://www.paulgraham.com/mit.html" } { "by": "phyllis", "descendants": 0, "id": 3, "kids": [ 454412, 531602 ], "score": 7, "time": 1160419233, "title": "Woz Interview: the early days of Apple", "type": "story", "url": "http://www.foundersatwork.com/stevewozniak.html" } { "by": "onebeerdave", "descendants": 0, "id": 4, "kids": [ 454413 ], "score": 5, "time": 1160419662, "title": "NYC Developer Dilemma", "type": "story", "url": "http://avc.blogs.com/a_vc/2006/10/the_nyc_develop.html" } { "by": "perler", "descendants": 0, "id": 5, "kids": [ 454414 ], "score": 7, "time": 1160419864, "title": "Google, YouTube acquisition announcement could come tonight", "type": "story", "url": "http://www.techcrunch.com/2006/10/09/google-youtube-sign-more-separate-deals/" } { "by": "perler", "descendants": 0, "id": 6, "kids": [ 454415 ], "score": 4, "time": 1160420200, "title": "Business Intelligence the Inkling Way: cool prediction markets software", "type": "story", "url": "http://360techblog.com/2006/10/02/business-intelligence-the-inkling-way/" } { "by": "phyllis", "descendants": 0, "id": 7, "kids": [ 454416 ], "score": 5, "time": 1160420455, "title": "Sevin Rosen Unfunds - why?", "type": "story", "url": "http://featured.gigaom.com/2006/10/09/sevin-rosen-unfunds-why/" } { "by": "frobnicate", "descendants": 0, "id": 8, "kids": [ 454417 ], "score": 10, "time": 1160421459, "title": "LikeBetter featured by BBC", "type": "story", "url": "http://news.bbc.co.uk/2/hi/programmes/click_online/5412216.stm" } { "by": "askjigga", "descendants": 0, "id": 9, "kids": [ 454418 ], "score": 4, "time": 1160421542, "title": "weekendr: social network for the weekend", "type": "story", "url": "http://www.weekendr.com/" } { "by": "frobnicate", "descendants": 0, "id": 10, "kids": [ 454419 ], "score": 3, "time": 1160421674, "title": "PhotoShow: Broadcast Photos to Cable TV", "type": "story", "url": "http://www.techcrunch.com/2006/10/09/broadcast-photos-to-cable-tv/" }

エラーが発生しているコード

<template> <div> <h1 class="website-title">Hacker News</h1> <div v-for="(article, index) in articles" :key="index"> <p>{{ article.id }}</p> </div> </div> </template> <script> export default { async asyncData({ $axios }) { const topStoryIds = await $axios.$get( 'https://hacker-news.firebaseio.com/v0/topstories.json' ) const topTenIds = topStoryIds.filter( (topStoryId) => topStoryIds.indexOf(topStoryId) <= 10 ) const articles = [] let articleId for (articleId in topTenIds) { const itemUrl = `https://hacker-news.firebaseio.com/v0/item/${articleId}.json?print=pretty` const item = await $axios.$get(itemUrl) articles.push(item) } return { articles, } }, } </script> <style scoped> .website-title { margin: 16px 16px; font-size: calc(1rem + 1vw); font-weight: bold; } </style>

よろしくお願いいたします。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

m.ts10806

2020/12/21 01:48

回答依頼いただきましたがNuxt.jsについては現時点で経験なしなので期待には添えません
spellbound

2020/12/21 01:53

承知しました!ご確認ありがとうございます!
guest

回答1

0

ベストアンサー

asyncDataの関数が実行されてないからかもです
訂正:されてるんですね><

投稿2020/12/21 04:15

編集2020/12/21 04:26
退会済みユーザー

退会済みユーザー

総合スコア0

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

退会済みユーザー

退会済みユーザー

2020/12/21 04:37 編集

この部分の、itemにnullあるんかもな const item = await $axios.$get(itemUrl) articles.push(item)
spellbound

2020/12/21 04:39

ありがとうございます!初期値に null が混ざっており、エラーが発生していました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問