#Content collections: no overload matches this call

6 messages · Page 1 of 1 (latest)

molten zodiac
#

I created a content collection in blog/*.mdx

in src/content.config.ts:

// 1. Import utilities from `astro:content`
import { defineCollection, z } from 'astro:content';

// 2. Import loader(s)
import { glob, file } from 'astro/loaders';

// 3. Define your collection(s)
const blog = defineCollection({
    loader: glob({ pattern: "**/*.mdx", base: "../blog" }),
    schema: z.object({
        title: z.string(),
        description: z.string(),
        pubDate: z.coerce.date(),
        updatedDate: z.coerce.date().optional(),
        languages: z.enum(["en", "nl"])
    })
});

// 4. Export a single `collections` object to register your collection(s)
export const collections = { blog };

in src/pages/index.astro getting this error:

---
import { getCollection } from "astro:content";
const allBlogPosts = await getCollection("blog");
---
No overload matches this call.
  Overload 1 of 2, '(collection: never, filter?: ((entry: never) => entry is never) | undefined): Promise<never[]>', gave the following error.
  Overload 2 of 2, '(collection: never, filter?: ((entry: never) => unknown) | undefined): Promise<never[]>', gave the following error.ts(2769)
inner raptorBOT
#

Hi @molten zodiac. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:

#

The error you're encountering is related to your content collection configuration. Let me help you troubleshoot this issue.

The Problem

The TypeScript error indicates that the getCollection() function can't find a valid collection named "blog". This typically happens when there's an issue with how your content collection is defined or where your content files are located.

Solution

There are a few issues to address:

  1. File Path Issue: Your base path in the loader is incorrect. The path should be relative to the project root, not to the content config file.

  2. File Location: Make sure your MDX files are in the correct location according to your configuration.

Here's how to fix your configuration:

// src/content.config.ts
import { defineCollection, z } from 'astro:content';
import { glob } from 'astro/loaders';

const blog = defineCollection({
    // Fix the base path - it should be relative to project root
    loader: glob({ pattern: "**/*.mdx", base: "./blog" }),
    schema: z.object({
        title: z.string(),
        description: z.string(),
        pubDate: z.coerce.date(),
        updatedDate: z.coerce.date().optional(),
        languages: z.enum(["en", "nl"])
    })
});

export const collections = { blog };

Additional Checks

  1. Restart the Dev Server: After making changes to your content configuration, you need to restart your dev server for Astro to recognize the changes Content Collections Guide.

  2. File Structure: Ensure your blog files exist in the correct location. Based on your configuration, they should be in a blog directory at the root of your project.

  3. TypeScript Configuration: Make sure your tsconfig.json has the necessary settings for content collections:

#
{
  "compilerOptions": {
    "strictNullChecks": true,
    "allowJs": true
  }
}
  1. Check for Syntax Errors: Ensure your MDX files have the correct frontmatter format that matches your schema.

If you're still having issues after these changes, you might want to check if your Astro version supports the Content Layer API (v5.0+). The newer Content Layer API provides better performance and scalability compared to the legacy API Upgrade to Astro v5.

Let me know if these changes resolve your issue!

GitHub: Kapa AI

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:

#

@molten zodiac Kapa.ai is still learning and improving, please let me know how I did by reacting below.

molten zodiac
#

it works now thanks. am i supposed to get auto-completion in my mdx file frontmatter?