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 'length' of undefined #155

Closed
joelgrodriguez opened this issue Apr 30, 2021 · 9 comments
Closed

TypeError: Cannot read property 'length' of undefined #155

joelgrodriguez opened this issue Apr 30, 2021 · 9 comments

Comments

@joelgrodriguez
Copy link

joelgrodriguez commented Apr 30, 2021

I just installed a fresh instance of WP and the starter.

The home page pulls up no problem but...

For some reason when I click on a post I am getting this error.

Server Error
TypeError: Cannot read property 'length' of undefined

`
147 |
148 | const { categories, postId } = post;

149 | const category = categories.length && categories[0];
| ^
150 | let { name, slug } = category;
151 |
152 | return {`

I have not added anything else to the starter (other than the graphql endpoint).
I have not added any posts new posts.
I added only one additional category for testing.

Is there a step I am missing?

Here is the WP install: http://newtechmachinery.kinsta.cloud/

thanks

@colbyfayock
Copy link
Owner

hey @joelgrodriguez i was able to reproduce, thats relaly strange

What results do you get when you run this query inside of the GraphiQL IDE?

{
  postBy(slug: "hello-world") {
    id
    title
    slug
  }
}

the issue is for some reason the query isn't finding the post by it's slug

@joelgrodriguez
Copy link
Author

This was returned:

{ "data": { "postBy": null }, "extensions": { "debug": [ { "type": "DEBUG_LOGS_INACTIVE", "message": "GraphQL Debug logging is not active. To see debug logs, GRAPHQL_DEBUG must be enabled." } ] } }

@colbyfayock
Copy link
Owner

@doingandlearning have you seen something like this before? any ideas?

the post metadata coming from the homepage has the hello-world slug

[
  {
    __typename: 'Post',
    author: {
      __typename: 'User',
      avatar: [Object],
      id: 'dXNlcjox',
      name: 'jrodriguez',
      slug: 'jrodriguez'
    },
    id: 'cG9zdDox',
    categories: [ [Object], [Object] ],
    content: '\n' +
      '<p>Welcome to WordPress. This is your first post. Edit or delete it, then start writing!</p>\n',
    date: '2021-04-30T02:13:28',
    excerpt: '<p>Welcome to WordPress. This is your first post. Edit or delete it, then start writing!</p>\n',
    featuredImage: null,
    modified: '2021-04-30 12:58:57',
    modifiedGmt: '2021-04-30T12:58:57',
    postId: 1,
    title: 'Hello world!',
    slug: 'hello-world',
    isSticky: false
  }
]

@joelgrodriguez
Copy link
Author

I'm thinking it may have something to do with Kinsta?

I just tested this on local install using Local by Flywheel and I am able to load the post with no errors.

@doingandlearning
Copy link
Collaborator

doingandlearning commented Apr 30, 2021

This is interesting. I think there was some talk about deprecating the by functions like postBy. It does indeed look like that's happened on your instance. This could be a WP version or WPGraphQL version.

To fix this in our code, we'd move from using postBy to using post. The new query would look like this:

{
  post(id: "hello-world", type: SLUG) {
    id
    title
    slug
  }
}

I'll send a message on the WPGraphQL Slack group about it to confirm if a new change has been made.

I note that when I use this new query on your instance @joelgrodriguez I get the data back as expected.

@joelgrodriguez
Copy link
Author

I was able to get the expected data back but with a modified query from yours @doingandlearning :

query MyQuery { post(id: "hello-world", idType: SLUG) { id title slug } }

The query provided above returned an error for me.

Thanks

@colbyfayock
Copy link
Owner

thanks for the heads up both of you, will get that updated to the new query format unless one of you wanna put in a PR :)

@doingandlearning
Copy link
Collaborator

Very happy to do this.

Confirmed with the WPGraphQL team that this was an issue.

Screenshot_20210430_171508_com Slack

@colbyfayock
Copy link
Owner

Fixed via #156

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

3 participants