如何编写 Strapi 查询以从类别内容类型获取我的帖子和图像

joh*_*zuo 1 strapi

我创建了一个集合类型“帖子”并创建了许多帖子。现在,我制作了另一个集合类型的类别,其标题为 ['softwaresApps','gifts','technology'] ,其中包含帖子关系。然后我尝试获取http://localhost:1337/api/categories?populate=*"并得到:

{
"data": [
    {
        "id": 1,
        "attributes": {
            "title": "Technology",
            "createdAt": "2022-01-09T00:43:05.899Z",
            "updatedAt": "2022-01-09T00:45:26.131Z",
            "publishedAt": "2022-01-09T00:44:04.298Z"
        }
    },
    {
        "id": 2,
        "attributes": {
            "title": "Gifts",
            "createdAt": "2022-01-09T00:43:48.196Z",
            "updatedAt": "2022-01-09T00:43:53.979Z",
            "publishedAt": "2022-01-09T00:43:53.961Z"
        }
    },
    {
        "id": 3,
        "attributes": {
            "title": "softwares&apps",
            "createdAt": "2022-01-09T00:48:23.706Z",
            "updatedAt": "2022-01-11T00:06:48.130Z",
            "publishedAt": "2022-01-09T00:49:59.571Z"
        }
    },
    {
        "id": 4,
        "attributes": {
            "title": "HomeSlider",
            "createdAt": "2022-01-15T15:06:23.122Z",
            "updatedAt": "2022-01-15T15:06:30.272Z",
            "publishedAt": "2022-01-15T15:06:30.259Z"
        }
    }
],
"meta": {
    "pagination": {
        "page": 1,
        "pageSize": 25,
        "pageCount": 1,
        "total": 4
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我似乎无法在数据中找到我的帖子关系。我需要一个查询来帮助我从类别中获取所有帖子和图像。

joh*_*zuo 7

我已经想通了。感谢@IceJonas,他的链接:\n /sf/answers/4917582911/

\n

事实证明,填充并http://localhost:1337/api/categories?populate=*"不会减少它。我找到了解决方案,您必须使用 -http://localhost:1337/api/categories?populate[posts][populate]=*并且您将填充所有关系,包括它们的图像。

\n
{\n"data": [\n    {\n        "id": 1,\n        "attributes": {\n            "softwareapps": "softwareapps",\n            "createdAt": "2022-01-18T01:42:01.223Z",\n            "updatedAt": "2022-01-18T01:43:37.935Z",\n            "publishedAt": "2022-01-18T01:43:37.931Z",\n            "posts": {\n                "data": [\n                    {\n                        "id": 3,\n                        "attributes": {\n                            "title": "The Best Drones for Photos and Video",\n                            "date": "2022-01-11",\n                            "article": "The **most** of your time with a _laptop_ is spent in a browser, you can get a better experience for your money with a [Chromebook.](wirecutter.com) \\n\\nNena Farrell has covered technology and connected home products since 2016, originally at Sunset Magazine (where she was an associate home editor) and now as an updates writer on the audio, visual, and smart-home team at Wirecutter.\\n\\nBrendan Nystedt contributed to an earlier version of this guide. He\xe2\x80\x99s been an enthusiast photographer and a writer covering consumer electronics and tech for the better part of a decade. Nystedt has worked for Wirecutter, Reviewed, and Wired, and he has written for numerous other outlets.\\n\\nWhile preparing this guide, we consulted reviews both from owners and from trustworthy outlets, such as Wired and PCMag. Unfortunately for the layperson shopping for these devices, there are many SEO-driven clickbait blogs offering hands-off reviews. We ignored those websites.\\n\\n",\n                            "createdAt": "2022-01-09T00:07:30.402Z",\n                            "updatedAt": "2022-01-16T23:27:06.589Z",\n                            "publishedAt": "2022-01-09T00:07:33.230Z",\n                            "author": "chisom ifem",\n                            "subCategory": "phone",\n                            "postId": "3",\n                            "readTime": "4",\n                            "category": "softwareapps",\n                            "popular": true,\n                            "homeStartHere": false,\n                            "slug": "The-Best-Drones-for-Photos-and-Video",\n                            "image": {\n                                "data": {\n                                    "id": 17,\n                                    "attributes": {\n                                        "name": "pg5.jpg",\n                                        "alternativeText": "pg5.jpg",\n                                        "caption": "pg5.jpg",\n                                        "width": 1470,\n                                        "height": 735,\n                                        "formats": {\n                                            "thumbnail": {\n                                                "name": "thumbnail_pg5.jpg",\n                                                "hash": "thumbnail_pg5_cbcb14de57",\n                                                "ext": ".jpg",\n                                                "mime": "image/jpeg",\n                                                "width": 245,\n                                                "height": 123,\n                                                "size": 12.24,\n                                                "path": null,\n                                                "url": "/uploads/thumbnail_pg5_cbcb14de57.jpg"\n                                            },\n                                            "large": {\n                                                "name": "large_pg5.jpg",\n                                                "hash": "large_pg5_cbcb14de57",\n                                                "ext": ".jpg",\n                                                "mime": "image/jpeg",\n                                                "width": 1000,\n                                                "height": 500,\n                                                "size": 63.18,\n                                                "path": null,\n                                                "url": "/uploads/large_pg5_cbcb14de57.jpg"\n                                            },\n                                            "medium": {\n                                                "name": "medium_pg5.jpg",\n                                                "hash": "medium_pg5_cbcb14de57",\n                                                "ext": ".jpg",\n                                                "mime": "image/jpeg",\n                                                "width": 750,\n                                                "height": 375,\n                                                "size": 39.5,\n                                                "path": null,\n                                                "url": "/uploads/medium_pg5_cbcb14de57.jpg"\n                                            },\n                                            "small": {\n                                                "name": "small_pg5.jpg",\n                                                "hash": "small_pg5_cbcb14de57",\n                                                "ext": ".jpg",\n                                                "mime": "image/jpeg",\n                                                "width": 500,\n                                                "height": 250,\n                                                "size": 23.05,\n                                                "path": null,\n                                                "url": "/uploads/small_pg5_cbcb14de57.jpg"\n                                            }\n                                        },\n                                        "hash": "pg5_cbcb14de57",\n                                        "ext": ".jpg",\n                                        "mime": "image/jpeg",\n                                        "size": 93.38,\n                                        "url": "/uploads/pg5_cbcb14de57.jpg",\n                                        "previewUrl": null,\n                                        "provider": "local",\n                                        "provider_metadata": null,\n                                        "createdAt": "2022-01-12T23:37:47.189Z",\n                                        "updatedAt": "2022-01-12T23:37:47.189Z"\n                                    }\n                                }\n                            }\n                        }\n                    },\n                    {\n                        "id": 2,\n                        "attributes": {\n                            "title": "The Best Mop-Vacuum Combo Is the Bissell CrossWave",\n                            "date": "2022-01-10",\n                            "article": "The Aeezo Portrait 01 is considerably cheaper than our other picks (it\xe2\x80\x99s nearly half the price of the Aura Mason), and it\xe2\x80\x99s surprisingly good. The frame is minimalist but doesn\xe2\x80\x99t look cheap, and the Aeezo has a unique feature that allows you to easily re-crop a photo so that no matter the orientation, it always looks good. The downside is the uploader, which limits you to 10 pictures at a time and is overall frustrating for the less-tech-savvy to navigate and understand.\\n\\n# This is the end hommies",\n                            "createdAt": "2022-01-09T00:00:30.997Z",\n                            "updatedAt": "2022-01-17T00:19:05.116Z",\n                            "publishedAt": "2022-01-11T22:24:52.541Z",\n                            "author": "Emeka olumma",\n                            "subCategory": "phone",\n                            "postId": "2",\n                            "readTime": "3",\n                            "category": "gifts",\n                            "popular": true,\n                            "homeStartHere": false,\n                            "slug": "The Best-Mop-Vacuum-Combo-is-the-Bissell-CrossWave",\n                            "image": {\n                                "data": {\n                                    "id": 21,\n                                    "attributes": {\n                                        "name": "slide2.jpg",\n                                        "alternativeText": "slide2.jpg",\n                                        "caption": "slide2.jpg",\n                                        "width": 1470,\n                                        "height": 735,\n                                        "formats": {\n                                            "thumbnail": {\n                                                "name": "thumbnail_slide2.jpg",\n                                                "hash": "thumbnail_slide2_de6b7dcd32",\n                                                "ext": ".jpg",\n                                                "mime": "image/jpeg",\n                                                "width": 245,\n                                                "height": 123,\n                                                "size": 11.51,\n                                                "path": null,\n                                                "url": "/uploads/thumbnail_slide2_de6b7dcd32.jpg"\n                                            },\n                                            "large": {\n                                                "name": "large_slide2.jpg",\n                                                "hash": "large_slide2_de6b7dcd32",\n                                                "ext": ".jpg",\n                                                "mime": "image/jpeg",\n                                                "width": 1000,\n                                                "height": 500,\n                                                "size": 43.34,\n                                                "path": null,\n                                                "url": "/uploads/large_slide2_de6b7dcd32.jpg"\n                                            },\n                                            "medium": {\n                                                "name": "medium_slide2.jpg",\n                                                "hash": "medium_slide2_de6b7dcd32",\n                                                "ext": ".jpg",\n                                                "mime": "image/jpeg",\n                                                "width": 750,\n                                                "height": 375,\n                                                "size": 30.53,\n                                                "path": null,\n                                                "url": "/uploads/medium_slide2_de6b7dcd32.jpg"\n                                            },\n                                            "small": {\n                                                "name": "small_slide2.jpg",\n                                                "hash": "small_slide2_de6b7dcd32",\n                                                "ext": ".jpg",\n                                                "mime": "image/jpeg",\n                                                "width": 500,\n                                                "height": 250,\n                                                "size": 19.62,\n                                                "path": null,\n                                                "url": "/uploads/small_slide2_de6b7dcd32.jpg"\n                                            }\n                                        },\n                                        "hash": "slide2_de6b7dcd32",\n                                        "ext": ".jpg",\n                                        "mime": "image/jpeg",\n                                        "size": 52.52,\n                                        "url": "/uploads/slide2_de6b7dcd32.jpg",\n                                        "previewUrl": null,\n                                        "provider": "local",\n                                        "provider_metadata": null,\n                                        "createdAt": "2022-01-15T15:33:30.925Z",\n                                        "updatedAt": "2022-01-15T15:33:30.925Z"\n                                    }\n                                }\n                            }\n                        }\n                    },\n                    {\n                        "id": 6,\n                        "attributes": {\n                            "title": "Laptop you can buy now in 2022",\n                            "date": "2022-01-14",\n                            "article": "A digital photo frame lets you easily add images\xe2\x80\x94including beautiful travel shots and family photos\xe2\x80\x94to the frame from anywhere. Whether you\xe2\x80\x99re giving a frame as a gift and plan to upload photos remotely, or you just want a great frame for yourself, the Aura Mason is the best frame we\xe2\x80\x99ve used. Its 8.57-inch display is sharp, bright, and vivid, and in our tests it was the simplest to set up. On top of that, it has a good-looking design.\\n\\nOf all the frames we tried, the Aura Mason came the closest to mimicking a regular photo frame. The Mason is straightforward to set up and use, and remotely loading photos is a breeze (the free Aura app works with Android and iOS phones). With this frame it is easy to add photos on the app or through a web uploader. From the intuitive design of the app to the frame\xe2\x80\x99s pared-down interface, the Mason is not just for the tech-savvy.\\n\\n\\n### The Aura Mason\\nThe Aura Mason Luxe is an upgrade to the Mason, with a larger price tag. That extra cost adds a slightly larger 2K screen and video capabilities onto everything we already like about the Mason frame. But even though the video capabilities are nice, we don\xe2\x80\x99t think they\xe2\x80\x99re truly necessary in order to enjoy a digital picture frame.\\n\\n\\n\\nThe [Aeezo Portrait](wirecutter.com) 01 is considerably cheaper than our other picks (it\xe2\x80\x99s nearly half the price of the Aura Mason), and it\xe2\x80\x99s surprisingly good. The frame is minimalist but doesn\xe2\x80\x99t look cheap, and the Aeezo has a unique feature that allows you to easily re-crop a photo so that no matter the orientation, it always looks good. The downside is the uploader, which limits you to 10 pictures at a time and is overall frustrating for the less-tech-savvy to navigate and understand.\\n\\n",\n                            "createdAt": "2022-01-09T00:18:35.467Z",\n                            "updatedAt": "2022-01-16T23:05:11.379Z",\n                            "publishedAt": "2022-01-09T00:18:38.306Z",\n                            "author": "John juzzy",\n                            "subCategory": "phone",\n                            "postId": "6",\n                            "readTime": "3",\n                            "category": "softwareapps",\n                            "popular": false,\n                            "homeStartHere": false,\n                            "slug": "Laptop-you-can-buy-now-in-2022",\n                            "image": {\n                                "data": {\n                                    "id": 13,\n                                    "attributes": {\n                                        "name": "kids3.png",\n                                        "alternativeText": "kids3.png",\n                                        "caption": "kids3.png",\n                                        "width": 206,\n                                        "height": 142,\n                                        "formats": null,\n                                        "hash": "kids3_eee4bde396",\n                                        "ext": ".png",\n                                        "mime": "image/png",\n                                        "size": 54.89,\n                                        "url": "/uploads/kids3_eee4bde396.png",\n                                        "previewUrl": null,\n                                        "provider": "local",\n                                        "provider_metadata": null,\n                                        "createdAt": "2022-01-09T00:16:21.166Z",\n                                        "updatedAt": "2022-01-09T00:16:21.166Z"\n                                    }\n                                }\n                            }\n                        }\n                    }\n                ]\n            }\n        }\n    }\n],\n"meta": {\n    "pagination": {\n        "page": 1,\n        "pageSize": 25,\n        "pageCount": 1,\n        "total": 1\n        }\n    }\n}\n
Run Code Online (Sandbox Code Playgroud)\n

我希望这可以帮助别人。

\n


归档时间:

查看次数:

2513 次

最近记录:

3 年,10 月 前