0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Astro|コンテンツコレクションの管理コンテンツを追加した際に発生するエラーと、その解消法(備忘録)

Posted at

概要

Astro フレームワークで コンテンツコレクションを追加時 に、追加したはずのコンテンツを認識してくれないエラーが発生した場合の解消法の備忘録。

事象

Astro で管理コンテンツを増やしたときに、以下のエラーが発生する場合がある。

エラー表示(例: "term" という利用規約用のコンテンツを追加したときに発生)

型 '"term"' は制約 'keyof ContentEntryMap' を満たしていません。 ts(2344)

astro_error_01.png

原因と対処法

パターン①:設定ファイルの未定義

src/content/config.ts に追加したコンテンツの設定を定義していない場合。
※ ただし、今回はこれは対応済みなので該当しなかった。

追加設定すべき定義

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

const termCollection = defineCollection({
  type: 'content',
  schema: z.object({
    title: z.string(),
    description: z.string().optional(),
    // 他の必要なフィールドを定義
  }),
});

export const collections = {
  term: termCollection,
  // 他のコレクション...
};

パターン②:コンテンツディレクトリの不在

対象となるディレクトリまたはその中の Markdown ファイルが存在しない場合。

これは、シンプルにディレクトリとファイルを作れば良い。

※ ただし、こちらも今回は事前に作成済みなので該当しなかった。

パターン③:TypeScript の型エラー

Astro の型生成が完了しておらず、CollectionEntry<"term"> の型が認識されていない場合。
今回は、これが該当した。

Astro の型生成コマンドを実行するだけで OK

npm run astro sync

→ これでエラーは消えた 🙌

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?