#The collection does not exist or is empty.

8 messages · Page 1 of 1 (latest)

sacred junco
#

Hey Kapa.

i'm trying to visit my "projects" collection from pages/gallery.astro but I'm getting the error "The collection "projects" does not exist or is empty. Please check your content config file for errors."

My data collection is inside src/portfolio, with pages listed post1.md, post2.md and so on

My src/content.config.cjs is set up like so:

import { glob } from 'astro/loaders';

const projects = defineCollection({
    loader: glob({pattern: "**/*.md", base: "src/portfolio"}), 
    schema : z.object({
        title: z.string(),
        description: z.string().optional(),
        summary: z.string().optional(),
        date: z.date(),
        weight: z.number(),
        featured_image: z.url(),
    }),
});

export const collections = { projects };```

my pages/gallery.astro is set up like so
```const pageTitle = "Portfolio";
const allPosts = await getCollection("projects");
console.log(allPosts);```

A typical markdown page in src/portfolio looks like so:

```---
title: "Test"
date: 2023-12-06
draft: false
language: en
summary: test.
description: Commission for john doe.
featured_image: ../assets/images/posts/post1.png
params:
  customer: john doe
weight: 01
---

Why is Astro not picking up the collection?

stoic stormBOT
#

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

#

Based on your description and the provided code, here are the most likely reasons why Astro is not picking up your "projects" collection:

1. File Extension for Content Config

Your config file is named src/content.config.cjs. According to the documentation, Astro expects the content config file to be named src/content.config.ts, src/content.config.js, or src/content.config.mjs (not .cjs). Using .cjs may prevent Astro from loading your collections at all. Try renaming your file to src/content.config.js or src/content.config.ts and restart the dev server after making this change Defining Collections.

2. Loader base Path

Your loader uses base: "src/portfolio". This is correct if your markdown files are in src/portfolio/. However, ensure that the path is correct and that there are no typos or case mismatches.

3. Schema Issues

  • The featured_image field in your schema is defined as z.url(), which expects a valid URL (e.g., <https://example.com/image.png>). In your markdown, you have a relative path (../assets/images/posts/post1.png). This will fail schema validation. Use z.string() instead for relative paths, or update your markdown to use absolute URLs Defining the collection schema.
  • The weight field in your markdown is 01, which is a string, but your schema expects a number. Make sure the value in your markdown is not quoted, i.e., weight: 1.

4. Required Fields

#

Your schema requires title, date, weight, and featured_image. If any markdown file is missing these or has them in the wrong format, the collection will fail to load, and Astro will report the collection as empty or invalid Content entry frontmatter does not match schema.

5. Restart the Dev Server

After making changes to your content config or collection files, you must restart the Astro dev server for changes to take effect Create a collection for your posts.


Summary of steps to fix:

  1. Rename src/content.config.cjs to src/content.config.js or src/content.config.ts.
  2. Change featured_image: z.url() to featured_image: z.string() in your schema, or use absolute URLs in your markdown.
  3. Make sure weight is a number in your markdown (not quoted).
  4. Ensure all required fields are present and correctly typed in every markdown file.
  5. Restart the Astro dev server.

If you follow these steps, your "projects" collection should be recognized by Astro. If you still encounter issues, check for error messages about schema validation, as they will point to the specific problem in your markdown files or schema Content entry frontmatter does not match schema.

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

#

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

sacred junco
#

@stoic storm Is there a way to sort the way posts are rendered from a content collection, by referencing the date of each post?

stoic stormBOT
#

Let me check the knowledge base for you again. One moment! :books:

#

Yes, you can sort the posts from a content collection by their date property after fetching them with getCollection(). The knowledge sources explain that the order of entries returned by getCollection() is non-deterministic, so if you need a specific order (such as by date), you must sort the collection entries yourself in your Astro component or page.

Here’s an example of how you might do this:

---
// Fetch all posts from the 'blog' collection
import { getCollection } from 'astro:content';
const posts = await getCollection('blog');

// Sort posts by date (newest first)
posts.sort((a, b) => b.data.pubDate - a.data.pubDate);
---

This approach works for any date field defined in your schema (e.g., pubDate or date). Just ensure that the field is a Date object in your schema so that the comparison works as expected Breaking changes to legacy content and data collections.

Note: You must perform the sorting manually, as Astro does not guarantee any default sort order for collection entries.

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