@utwo4805

Are you sure you want to delete the question?

If your question is resolved, you may close it.

Leaving a resolved question undeleted may help others!

We hope you find it useful!

Microcmsでタグ別の記事のデータを取得したい

タグ別の記事のデータを取得したい

AstroとMicrocmsでブログサイトを作成しており
タグ別の記事一覧を作成するために
全てのタグを取得し、その取得したタグを元に
タグ別に記事データを取得しようと下記のコードを書きました。

export async function getStaticPaths(){

    //記事データを取得
    const response = await getBlogs({});
    const tags = response.contents;
    
    //全てのタグを取得
    const tagList = tags.map((tag) => {
        return tag.id;
    });
    //tagListの中身
    [ 'ssz9g857d0b', 'inexlbbch3' ]

    
    //タグごとの記事を取得 
    const getBlogsByTags = async (queries?: MicroCMSQueries) => {
    return await client.get<BlogResponse>({ endpoint: "blogs", queries:{filters: `tag[contains]${tagList}`}});
    };
    const result = await getBlogsByTags({})
    
    console.log(result)


}

出力結果
{ contents: [], totalCount: 0, offset: 0, limit: 10 }

発生している問題

下記のコードで[contains]で指定して複数指定を行っているのですがcontentsが空の配列で返されてしまいます。

return await client.get<BlogResponse>({ endpoint: "blogs", queries:{filters: `tag[contains]${tagList}`}});
    };

試したこと

[ 'ssz9g857d0b', 'inexlbbch3' ]配列の状態だどfilterがうまく作用しない可能性があるとかんがえたので文字列で指定をしましたがうまくいきまんせした。

 const getBlogsByTags = async (queries?: MicroCMSQueries) => {
    return await client.get<BlogResponse>({ endpoint: "blogs", queries:{filters: `tag[contains]${tagList.join(",")}`}});
    };

単数の場合([equals])は問題なくコンテンツデータを取得できるのですが
複数含ませた場合がうまく取得できません。
どなたか分かる方いらっしゃいましたら、ご回答いただけますと幸いです。

0 likes

1Answer

sが抜けていませんか?

- return await client.get<BlogResponse>({ endpoint: "blogs", queries:{filters: `tag[contains]${tagList}`}});
+ return await client.get<BlogResponse>({ endpoint: "blogs", queries:{filters: `tags[contains]${tagList}`}});
0Like

Comments

  1. @utwo4805

    Questioner

    tagsに変更してみたのですが出力結果は変わらず
    { contents: [], totalCount: 0, offset: 0, limit: 10 }でした。

    jsonは下記なのでtagでいけるかなと思ったのですが。
    {
    "contents": [
    {
    "id": "vijxcrhnt",
    "createdAt": "2023-12-15T11:11:59.131Z",
    "updatedAt": "2023-12-18T13:16:32.765Z",
    "publishedAt": "2023-12-15T11:11:59.131Z",
    "revisedAt": "2023-12-18T13:16:32.765Z",
    "eyecatch": {
    "url": "https://images.microcms-assets.io/assets/adf94cb4cf4e4e6cbf458fc4fc199e60/80c3231d1bb142fcb4314f5946e37ccd/blog-template.png",
    "height": 630,
    "width": 1200
    },
    "title": "テスト記事②",
    "tag": {
    "id": "ssz9g857d0b",
    "createdAt": "2023-12-15T11:04:17.198Z",
    "updatedAt": "2023-12-15T11:04:17.198Z",
    "publishedAt": "2023-12-15T11:04:17.198Z",
    "revisedAt": "2023-12-15T11:04:17.198Z",
    "name": "CSS"
    },
    "content": [
    {
    "fieldId": "ritchEditor",
    "html": "

    あいうえお

    "
    }
    ],
    "category": {
    "id": "22_x1awmu-c8",
    "createdAt": "2023-12-18T13:16:21.076Z",
    "updatedAt": "2023-12-18T13:16:21.076Z",
    "publishedAt": "2023-12-18T13:16:21.076Z",
    "revisedAt": "2023-12-18T13:16:21.076Z",
    "category": "webのこと"
    }
    },
    {
    "id": "eawipo07_i6",
    "createdAt": "2023-12-15T10:59:51.257Z",
    "updatedAt": "2023-12-15T10:59:51.257Z",
    "publishedAt": "2023-12-15T10:59:51.257Z",
    "revisedAt": "2023-12-15T10:59:51.257Z",
    "eyecatch": {
    "url": "https://images.microcms-assets.io/assets/adf94cb4cf4e4e6cbf458fc4fc199e60/80c3231d1bb142fcb4314f5946e37ccd/blog-template.png",
    "height": 630,
    "width": 1200
    },
    "title": "テスト記事です。",
    "tag": {
    "id": "inexlbbch3",
    "createdAt": "2023-12-15T10:59:14.999Z",
    "updatedAt": "2023-12-15T10:59:14.999Z",
    "publishedAt": "2023-12-15T10:59:14.999Z",
    "revisedAt": "2023-12-15T10:59:14.999Z",
    "name": "HTML"
    },
    "content": [
    {
    "fieldId": "ritchEditor",
    "html": "

    こんにちは

    "
    }
    ],
    "category": null
    }
    ],
    "totalCount": 2,
    "offset": 0,
    "limit": 10
    }

    お力貸してくださいまして、ありがとうございました。
    引き続き試行錯誤してみます。

  2. jsonは下記なのでtagでいけるかなと思ったのですが。

    上のJSONならidでは?

    id: ["vijxcrhnt", "eawipo07_i6"]

Your answer might help someone💌