javascript - Contentful with Nuxt.JS "Expected parameter accessToken" - Stack Overflow

I made a page which pulls data from Contentful. The data is pulling correctly, but buttons which use fu

I made a page which pulls data from Contentful. The data is pulling correctly, but buttons which use functions from methods don't work. Live updating of variables (for example, using v-model) doesn't work either.

I see this error in the console:

I think this error is the problem. Does anyone know what's wrong? I have no clue how to solve it :(

My contentful.js:

const contentful = require('contentful')

const client = contentful.createClient({
  space: process.env.CONTENTFUL_ENV_SPACE_ID,
  accessToken: process.env.CONTENTFUL_ENV_ACCESS_TOKEN
})

module.exports = client

Code which pulls data:

export default {
  layout: "landing_page",
  asyncData() {
    return client
      .getEntries({
        content_type: "landingPage"
      })
      .then(entries => {
        return { contentfulData: entries.items[0].fields };
      });
  },
  puted: {
    styles() {
      return landingPageCss;
    }
  },
  ponents: {
    priceBox,
    contact,
    home,
    aboutUs,
    footerDiv
  }
};

I made a page which pulls data from Contentful. The data is pulling correctly, but buttons which use functions from methods don't work. Live updating of variables (for example, using v-model) doesn't work either.

I see this error in the console:

I think this error is the problem. Does anyone know what's wrong? I have no clue how to solve it :(

My contentful.js:

const contentful = require('contentful')

const client = contentful.createClient({
  space: process.env.CONTENTFUL_ENV_SPACE_ID,
  accessToken: process.env.CONTENTFUL_ENV_ACCESS_TOKEN
})

module.exports = client

Code which pulls data:

export default {
  layout: "landing_page",
  asyncData() {
    return client
      .getEntries({
        content_type: "landingPage"
      })
      .then(entries => {
        return { contentfulData: entries.items[0].fields };
      });
  },
  puted: {
    styles() {
      return landingPageCss;
    }
  },
  ponents: {
    priceBox,
    contact,
    home,
    aboutUs,
    footerDiv
  }
};
Share Improve this question edited Mar 22, 2020 at 10:39 Dan 63.2k18 gold badges111 silver badges119 bronze badges asked Mar 21, 2020 at 20:28 BobiDaHombreBobiDaHombre 2331 gold badge5 silver badges20 bronze badges 5
  • have you tried outputting the contents of process.env.CONTENTFUL_ENV_ACCESS_TOKEN? Maybe the environment isn't being loaded correctly? – Marcus Ilgner Commented Mar 21, 2020 at 20:38
  • @milgner Yes, it doesn't work :( – BobiDaHombre Commented Mar 21, 2020 at 23:09
  • So the question is: how are you setting the environment? Are you using https://www.npmjs./package/dotenv? Via another mechanism? – Marcus Ilgner Commented Mar 21, 2020 at 23:12
  • Yes, I use dotenv – BobiDaHombre Commented Mar 22, 2020 at 2:08
  • Okay but then your question isn't about Nuxt or the Contentful API but about your environment not being loaded correctly? You should add some code about how you're loading dotenv and maybe a redacted version of your .env file? – Marcus Ilgner Commented Mar 22, 2020 at 13:40
Add a ment  | 

2 Answers 2

Reset to default 5

The best approach is used dotenv package to that. Set your env keys in .env file.

nuxt.config.js file should contain:

const env = require('dotenv').config()

export default {
  mode: 'universal',
  ...
  env: env.parsed,
  ...
}

Look at this video: https://codecourse./watch/using-env-files-with-nuxt

If you use dotenv you need to do following steps:

npm install --save-dev @nuxtjs/dotenv

Then you install it as an module. Note here if you using Nuxt.js older then v2.9 then you ahve to go to nuxt.config.js and put your code into the module section:

...
   module: [
   '@nuxtjs/dotenv'
 ]
...

If there is no module section then create one.

If you using newer then v2.9 then you put it into the buildModules

  ...
   buildModules: [
   '@nuxtjs/dotenv'
 ]
...

Your variables that are saved in the .env file are now accessable through context.env or process.env

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745123451a4612552.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信