https://izm51.com/posts/nuxt-contentful-netlify-blog-making-1/
を見ながらNuxt+ContentfulのCMSをやろうとしたのですが、アクセストークンでエラーになります。
{{ post.fields.title }}
の部分が表示されません。。
index.vue
javascript
1<template> 2 <div class="posts"> 3 <div v-for="(post, index) in posts" :key="index" class="post"> 4 {{ post.fields.title }} 5 </div> 6 </div> 7</template> 8 9<script> 10import client from '~/plugins/contentful' 11export default { 12 asyncData({ params }) { 13 return client 14 .getEntries({ 15 content_type: 'post', 16 order: '-sys.createdAt', 17 }) 18 .then(entries => { 19 return { posts: entries.items } 20 }) 21 .catch(e => console.log(e)) 22 }, 23 head: { 24 title: 'latest Posts', 25 }, 26 methods: { 27 formatData(iso) { 28 const date = new Date(iso) 29 const yyyy = new String(data.getFullYear()) 30 const mm = new String(data.getMonth() + 1).padStart(2, "0") 31 const dd = new String(date.getDate()).padStart(2,"0") 32 return `${yyyy}.${mm}.${dd}` 33 } 34 }, 35 //methods "posts" is not defined on the ~~が出るので追加してます>< 36 props: { 37 posts:{ 38 type:String 39 } 40 } 41} 42</script>
nuxt.config.js
javascript
1require('dotenv').config() 2const client = require('./plugins/contentful') 3 4module.exports = { 5 head: { 6 title: 'nuxt-cms', 7 meta: [ 8 { charset: 'utf-8' }, 9 { name: 'viewport', content: 'width=device-width, initial-scale=1' }, 10 { hid: 'description', name: 'description', content: 'Nuxt.js project' } 11 ], 12 link: [ 13 { rel: 'icon', type: 'image/x-icon', href: '/favicon.ico' } 14 ] 15 }, 16 loading: { color: '#3B8070' }, 17 build: { 18 extend (config, { isDev, isClient }) { 19 if (isDev && isClient) { 20 config.module.rules.push({ 21 enforce: 'pre', 22 test: /.(js|vue)$/, 23 loader: 'eslint-loader', 24 exclude: /(node_modules)/ 25 }) 26 } 27 } 28 }, 29 plugins: [ 30 '~/plugins/contentful' 31 ], 32 modules: [ 33 '@nuxtjs/dotenv', 34 '@nuxtjs/markdownit' 35 ], 36 markdownit: { 37 injected: true, 38 html: true, 39 linkify: true, 40 typography: true, 41 }, 42 generate: { 43 routes() { 44 return client 45 .getEntries({ content_type: 'post'}) 46 .then(entries => { 47 return entries.items.map(entry => { 48 return { 49 route: "/posts/"+entry.fields.slug, 50 payload: entr 51 } 52 }) 53 }) 54 } 55 }, 56 env: { 57 CTF_SPACE_ID: process.env.CTF_SPACE_ID, 58 CTF_CDA_ACCESS_TOKEN: process.env.CTF_CDA_ACCESS_TOKEN, 59 } 60}
contentful.js
javascript
1const contentful = require("contentful") 2const config = { 3 space: process.env.CTF_SPACE_ID, 4 accessToken: process.env.CTF_CDA_ACCESS_TOKEN 5 } 6 const client = contentful.createClient(config) 7 export default client
contentfulで設定したAPIキーから直接curlしても↓になるので(参考: https://www.contentful.com/developers/docs/references/authentication/ )Nuxt側ではなくContentful側の問題かな、、とも思ってますがよくわかりません。。お手上げです><
javascript
1# Request 2curl https://cdn.contentful.com/spaces/cfexampleapi/entries?access_token=wrong 3 4# Response 5{ 6 "sys": { 7 "type": "Error", 8 "id": "AccessTokenInvalid" 9 }, 10 "message": "An access token is required. Please send one through the HTTP Authorization header or as the query parameter \"access_token\".", 11 "requestId": "念の為伏せます" 12}
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/11/27 09:24