#content collection + dynamic routing and filtered Posts/Projects

4 messages · Page 1 of 1 (latest)

undone burrow
#

I am trying for hours now to generate "categories" pages. I have one category per project(post) (string)

i think i am very close since the output of console.log(projects) shows only data from projects for the category, yet i get an error.

Cannot read properties of undefined (reading 'title')

Can someone nudge me in the right direction? Am I overthinking something? can this be done with the getCollection definition already maybe?

export async function getStaticPaths(){
const allProjects = await getCollection('projects');

const cats: string [] = [];

allProjects.forEach((proj) => {
  const cat = proj.data.category.toLowerCase();
  cats.push(cat);
})

return Array.from(new Set(cats)).map((cat) => {
  return {
    params: { cat },
    props: {
      cat,
      projects: allProjects.filter((proj) => proj.data.category.includes(cat)),
      },
    };
  });
}
type Props = CollectionEntry<'projects'>;
  // const { cat } = Astro.params;
  const projects = Astro.props;
  console.log(projects);
  const p = projects.data;
  const titles = projects.data.title;
dawn pilot
#

here my demo example using content collections https://github.com/MicroWebStacks/astro-examples/tree/main#12_content-collections
it features to following

  • integration : <@&1055234544183287879>/node
  • adapter : node-standalone
  • Content Collections
  • get all frontmatter meta data with getCollection()
  • rendering of multiple Markdown pages in one page with await Promise.all() and item.render()
  • Single pages rendering in a dynamic path [...slug].astro with getEntryBySlug()

if this still does not help you can think of sharing a repo or stackbliz with your example and make it as minimal as possible.

undone burrow
#

thank you very much this was very helpful, i finally figured it out! really appreciate the help, i know it's a primitive problem, but as a newbie its sometimes tricky 🙂

undone burrow
#

can i ask u one more "stupid" question: i am using netlify-cms and defined my collections in astro.config.mjs already. from my lsp i get all the suggestions for astro collections already without defining a config.ts in the contents folder. Is this intended, or did i do sth freaky by accident? I don't complain, i just comprehend the astro magic i guess if this is whats supposed to happen. I guess it doesn't matter where astro sources the collections - right?