I’m finding some weird behaviour when trying to get content from a Custom Post Type (case_studies). When I start the site with npx frontity dev I get a “Cannot read property of 89” where 89 is the ID of the post type. If I run in the console frontity.state.source and then refresh the page, it loads fine and no more error shows.
It might be that the post type has not been fetched yet and the ID is not ready.
I have added the post type under postTypes in frontity.settings.js
If you’ve defined the post type in frontity.settings.js then you shouldn’t need to use useEffect and actions.source.fetch() in order to get the posts. Just providing a link to the archive address should load the posts of that post type.
See this example project which demonstrates using custom post types in a Frontity project.
If you still need more help after studying the example project then please provide a link to a repo. See here for the info that you can provide that will help us to best help you.
Thanks for providing the repo. Because you’re working with CPTs we can’t use one of our demo WordPress installations, so it would be useful for us if your WordPress site (or a version with the CPT) was also publicly available. Thanks.
Actually, on second thoughts, no you shouldn’t have to create a taxonomy. If you look at the example we use in our step-by-step tutorial, although it has a taxonomy, it’s not being used as the value of the archive property.
Try adding the postTypes settings from your original post to the frontity.settings.js of a new project using mars-theme. Does it work then, or do you still have the same problem there?
I’ve just tested the postTypes on the mars-theme but without the await actions.source.fetch(/case_studies/) in the theme’s beforeSSR it breaks and throws the error Cannot read property '89' of undefined
What happens if you use the slug of the post instead of the ID? Frontity works with URLs so unless you have a specific use case use the permalinks rather than the IDs.
Also what happens if you use the /case_studies endpoint, so if you put https://yourdomain.com/case_studies into your address bar? Do you get a listing of the case_studies post type?
In particular try this without any changes to mars-theme apart from adding the post type info to frontity.settings.js.
Again I refer you to this page in the step-by-step tutorial. That should be all you need to do to get basic functionality for CPTs, and you can extend and/or elaborate from there.
because the case_studies array key is the ID of the case study post
Is there any other way to retrieve each post type?
Will I eventually incur in some issues if I keep the await actions.source.fetch(/case_studies/) in the beforeSSR action in my theme? With that in it seems to be working fine.
In JavaScript almost everything is an object, null and undefined are exceptions. This error occurs when a property is read or a function is called on an undefined variable. Undefined means that a variable has been declared but has not been assigned a value. In JavaScript, properties and functions can only belong to objects. Since undefined is not an object type, calling a function or a property on such a variable causes the TypeError: Cannot read property of undefined.
If you are not sure a variable that will always have some value, the best practice is to check the value of variables for null or undefined before using them. To avoid getting these types of errors, you need to make sure that the variables you are trying to read do have the correct value. This can be done in various ways. You can do if checks before dealing with objects whose values are bound to change: