#Cannot read properties of undefined (reading 'includes')

1 messages · Page 1 of 1 (latest)

raw ridge
#

I'm getting the error Cannot read properties of undefined (reading 'includes') on the following code, I can't seem to see what the problem is - I have this working on another project:

---
// @ts-nocheck
import { getCollection } from "astro:content";
import slugify from "slugify";
import CategoryLayout from "../../layouts/CategoryLayout.astro";

export async function getStaticPaths() {
  const locations = await getCollection("locations");
  const tags = [...new Set(locations.map((location) => location.data.tags).flat())];
  return tags.map((tag) => {
    const tagged = locations.filter((location) => location.data.tags.includes(tag));
    return {
      params: { tag: slugify(tag, { lower: true }) },
      props: { tag: tag, locations: tagged }
    };
  });
}

const { tag, locations } = Astro.props;
---

<CategoryLayout title={tag} description="">
</CategoryLayout>

The entry in my content collection looks like:

---
title: Page
description: Desc
tags: [Historic Sites]
---
raw ridge
#

I've just removed all references to that line to troubleshoot, am getting slugify: string argument expected now.

fickle apex
#

Your initial error is saying that in this line:
location.data.tags.includes(tag), tags is undefined.
So you could change it to:
location.data.tags?.includes(tag)
or:
location.data.tags && location.data.tags.includes(tag)

#

Also, arrays in yaml are like this:

---
title: Page
description: Desc
tags: 
  - Historic Sites
---
raw ridge
fickle apex
#

Yeah, see my last comment

raw ridge
#

You’re referring to block sequence, both are valid.

fickle apex
#

Well... the error says tags is undefined, so maybe one doesn't have tags or there's some typo then.

raw ridge
fickle apex
#

Or an error in your collection schema?

raw ridge
#

#

I haven’t defined the tags in the content collection!

raw ridge
fickle apex
#

Ah, it's always the little things. 🤔

raw ridge