前提・実現したいこと
contentful から media を取得して nuxt で制作中のページに表示させる
発生している問題・エラーメッセージ
発生している現象は以下のサイトの方と同じです
https://github.com/KyleTryon/VidFireBlog/issues/1
Cannot read property 'fields' of undefined
https://note.mu/koushikagawa/n/na916f2b46baa
上の方と同様に実装を行ったのですが、post.fields.titleやpost.sys.createdAtなどは取得できるのにもかかわらず、post.fields.imgより以下の階層のものが取得できません
なお、post.fileds.imgだけで出力されたものはいかになります
{ "sys": { "space": { "sys": { "type": "Link", "linkType": "Space", "id": "3hasb61c1mn6" } }, "id": "4kaSk0Xd6ClxxSfTKTUJD3", "type": "Asset", "createdAt": "2019-01-19T06:38:05.712Z", "updatedAt": "2019-01-19T06:38:05.712Z", "environment": { "sys": { "id": "master", "type": "Link", "linkType": "Environment" } }, "revision": 1, "locale": "en-US" }, "fields": { "title": "スクリーンショット 2018-12-17 23.06.59", "file": { "url": "//images.ctfassets.net/3hasb61c1mn6/4kaSk0Xd6ClxxSfTKTUJD3/053bf5e2ed126e8054adaf0401a0c48c/____________________________2018-12-17_23.06.59.png", "details": { "size": 80752, "image": { "width": 1024, "height": 689 } }, "fileName": "スクリーンショット 2018-12-17 23.06.59.png", "contentType": "image/png" } } }
post.fields.img.fields.file.url
で画像のURLを取得できると思ったのですが、上記のエラーが出てきてしまいます
なにか取得する方法はあるのでしょうか?
該当のソースコード
親ページ
vue
1<template lang="pug"> 2 div 3 sub-header(title="お知らせ" desc="////////") 4 bread-crumb 5 .bg 6 .wrapper 7 article-card(v-for="(post, i) in posts" :key="post.key" :title="post.fields.title" :body="post.fields.body" :created="post.sys.createdAt" :updated="post.sys.updatedAt" :photo="post.fields.img") 8</template> 9 10<script> 11import SubHeader from '~/components/SubHeader.vue' 12import ArticleCard from '~/components/ArticleCard.vue' 13import BreadCrumb from '~/components/BreadCrumb.vue' 14import { createClient } from '~/plugins/contentful.js' 15 16const client = createClient() 17export default { 18 components: { 19 SubHeader, 20 ArticleCard, 21 BreadCrumb 22 }, 23 asyncData({env, params}) { 24 return client 25 .getEntries(env.CTF_BLOG_POST_TYPE_ID) 26 .then(entries => { 27 return { 28 posts: entries.items 29 } 30 }) 31 .catch(console.error) 32 } 33} 34</script> 35 36<style lang="sass" scoped> 37div 38 .bg 39 background-color: #fff 40 padding: 1em .5em 41 .wrapper 42 width: 1000px 43 margin: 0 auto 44</style>
コンポーネント
vue
1<template lang="pug"> 2.article-card 3 p {{title}} 4 hr 5 vue-markdown {{body}} 6 hr 7 p {{created}} 8 hr 9 p {{updated}} 10 hr 11 p {{photo}} 12</template> 13 14<script> 15import VueMarkdown from 'vue-markdown' 16 17export default { 18 data: function() { 19 return { 20 imgUrl: null 21 } 22 }, 23 props: [ 24 'title', 25 'body', 26 'created', 27 'updated', 28 'photo' 29 ], 30 components: { 31 VueMarkdown 32 }, 33 created: function() { 34 this.imgUrl = this.photo 35 } 36} 37</script> 38 39<style lang="sass" scoped> 40.article-card 41 width: 100% 42 border: #ddd 2px solid 43 border-radius: 3px 44 margin-bottom: .5em 45 padding: .5em 46 overflow: hidden 47</style>
試したこと
vueでcontentfulを用いて画像を取得されている方の記事は一通りみて、いろいろな方法で取得を試みましたが、解決しませんでした。
その他の試したことは以下の方とすべて同じです。
https://github.com/KyleTryon/VidFireBlog/issues/1
原因がわかる方がいらっしゃるようでしたら、答えていただけると幸いです。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/03/29 07:09
2019/03/29 07:21
2019/03/29 07:22
2019/03/29 08:12
2019/03/29 08:15
2019/03/29 08:23