Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeError: Cannot read property 'getContentTypes' of undefined #105

Open
AlexandreVerhoye opened this issue Nov 2, 2021 · 6 comments
Open

Comments

@AlexandreVerhoye
Copy link

AlexandreVerhoye commented Nov 2, 2021

Hello ! First of all thanks for this package, It will be really useful to me 👍

I have this issue while trying to generate types. I created the getContentfulEnvironment in the root of the project like that :

const contentfulManagement = require("contentful-management");

module.exports = function () {
  const contentfulClient = contentfulManagement.createClient({
    accessToken: "myaccesstoken",
    host: "cdn.contentful.com",
  });

  return contentfulClient.getSpace("myspaceid").then((space) => {
    space.getEnvironment("master");
  });
};

then I just ran npm run contentful-typescript-codegen and I get this error :

TypeError: Cannot read property 'getContentTypes' of undefined

Thanks and have a great day !

@sahildeliwala
Copy link

sahildeliwala commented Nov 10, 2021

@AlexandreVerhoye it seems you need to return space.getEnvironment("master")

can you try this?

const contentfulManagement = require("contentful-management");

module.exports = function () {
  const contentfulClient = contentfulManagement.createClient({
    accessToken: "myaccesstoken",
    host: "cdn.contentful.com",
  });

  return contentfulClient.getSpace("myspaceid").then((space) => {
    return space.getEnvironment("master");
  });
};

@AlexandreVerhoye
Copy link
Author

Hey, now I have this error :

NotFound: {
  "status": 404,
  "statusText": "Not Found",
  "message": "The resource could not be found.",
  "details": {},
  "request": {
    "url": "/spaces/xxxxxxxxx/environments/master",
    "headers": {
      "Accept": "application/json, text/plain, */*",
      "Content-Type": "application/vnd.contentful.management.v1+json",
      "X-Contentful-User-Agent": "sdk contentful-management.js/7.45.0; platform node.js/v14.18.1; os macOS/21.1.0;",
      "Authorization": "Bearer xxxxx",
      "user-agent": "node.js/v14.18.1",
      "Accept-Encoding": "gzip"
    },
    "method": "get"
  },
  "requestId": "xxxxxx"
}

@sahildeliwala
Copy link

@AlexandreVerhoye hmmm... can you try just this?
and make sure the Access token you add is your Personal Access Token.
omit host and try.

const contentfulManagement = require("contentful-management");

module.exports = function () {
  const contentfulClient = contentfulManagement.createClient({
    accessToken: "myaccesstoken", 
  });

  return contentfulClient.getSpace("myspaceid").then((space) => {
    return space.getEnvironment("master");
  });
};

@AlexandreVerhoye
Copy link
Author

I'm getting this error :

AccessTokenInvalid: {
  "status": 403,
  "statusText": "Forbidden",
  "message": "The access token you sent could not be found or is invalid.",
  "details": {},
  "request": {
    "url": "/spaces/xxxxx",
    "headers": {
      "Accept": "application/json, text/plain, */*",
      "Content-Type": "application/vnd.contentful.management.v1+json",
      "X-Contentful-User-Agent": "sdk contentful-management.js/7.45.0; platform node.js/v14.18.1; os macOS/21.1.0;",
      "Authorization": "Bearer ...xxxx",
      "user-agent": "node.js/v14.18.1",
      "Accept-Encoding": "gzip"
    },
    "method": "get"
  },
  "requestId": "c1d77801-035f-4a69-b6de-3ba849d66d38"
}

They are the same credential I'm using to fetch data on my nextjs project

@jaec0113
Copy link

jaec0113 commented Dec 6, 2021

Based on my experience (and another issue posted here), it seems like if you are trying to hide your info in an environment variable and then access them in the .getContentfulEvironment.js file then it doesn't work properly (I got the exact same error messages). Once I hard entered the token, space, and environment names into the file it worked perfectly.

@KacperBiedka
Copy link

Might be late for this, but in my case it was the difference between tokens, the contentful-management library requires a different type of token that you can find under this section in settings > api keys

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants