【问题标题】:Problem getting Image URL with Strapi / Nuxt使用 Strapi / Nuxt 获取图像 URL 时出现问题
【发布时间】:2021-03-30 03:34:56
【问题描述】:

我正在尝试从调用我的strapi 内容获得的 JSON 中获取图像 URL。这是一个活动网站。这是代码。当我控制台记录图像 url 时,我得到“未定义”。但是,事件的所有其他属性都没有问题。

created() {
axios.get('http://localhost:1338/events').then(res => {
this.events = res.data.sort((a, b) => b.date < a.date ? 1: -1);
console.log(this.events[0].image.url)
})
.catch(err => console.log(err))
}

控制台:未定义

这是 JSON:

{
    "id": 1,
    "title": "Aphex Twin Live Set",
    "description": "Risus viverra adipiscing at in tellus integer feugiat scelerisque varius.",
    "date": "2020-12-23T16:00:00.000Z",
    "price": 10,
    "published_at": "2020-12-19T17:25:44.340Z",
    "created_at": "2020-12-19T17:25:41.833Z",
    "updated_at": "2020-12-19T21:42:15.696Z",
    "image": [
        {
            "id": 1,
            "name": "aphex.jpeg",
            "alternativeText": "",
            "caption": "",
            "width": 1200,
            "height": 630,
            "formats": {
                "thumbnail": {
                    "name": "thumbnail_aphex.jpeg",
                    "hash": "thumbnail_aphex_11b34d4058",
                    "ext": ".jpeg",
                    "mime": "image/jpeg",
                    "width": 245,
                    "height": 129,
                    "size": 4.12,
                    "path": null,
                    "url": "/uploads/thumbnail_aphex_11b34d4058.jpeg"
                },
                "large": {
                    "name": "large_aphex.jpeg",
                    "hash": "large_aphex_11b34d4058",
                    "ext": ".jpeg",
                    "mime": "image/jpeg",
                    "width": 1000,
                    "height": 525,
                    "size": 28.49,
                    "path": null,
                    "url": "/uploads/large_aphex_11b34d4058.jpeg"
                },
                "medium": {
                    "name": "medium_aphex.jpeg",
                    "hash": "medium_aphex_11b34d4058",
                    "ext": ".jpeg",
                    "mime": "image/jpeg",
                    "width": 750,
                    "height": 394,
                    "size": 18.95,
                    "path": null,
                    "url": "/uploads/medium_aphex_11b34d4058.jpeg"
                },
                "small": {
                    "name": "small_aphex.jpeg",
                    "hash": "small_aphex_11b34d4058",
                    "ext": ".jpeg",
                    "mime": "image/jpeg",
                    "width": 500,
                    "height": 263,
                    "size": 10.67,
                    "path": null,
                    "url": "/uploads/small_aphex_11b34d4058.jpeg"
                }
            },
            "hash": "aphex_11b34d4058",
            "ext": ".jpeg",
            "mime": "image/jpeg",
            "size": 37.07,
            "url": "/uploads/aphex_11b34d4058.jpeg",
            "previewUrl": null,
            "provider": "local",
            "provider_metadata": null,
            "created_at": "2020-12-19T17:25:35.172Z",
            "updated_at": "2020-12-19T17:25:35.183Z"
        }
    ],
    "categories": [
        {
            "id": 1,
            "name": "Music",
            "published_at": "2020-12-19T21:18:16.946Z",
            "created_at": "2020-12-19T21:18:13.393Z",
            "updated_at": "2020-12-19T21:18:16.961Z"
        }
    ],
    "artists": [
        {
            "id": 1,
            "name": "Aphex Twin",
            "bio": "Richard James is a pretty old guy by now but he was banging in the techno/ambient electronic scene in the late 80s and 90s. He's from Cornwall so must've been pretty bored on most days, which lead him to hack at computers and electronic sound generators. The rest is history.",
            "published_at": "2020-12-19T21:42:33.225Z",
            "created_at": "2020-12-19T21:42:03.445Z",
            "updated_at": "2020-12-19T21:42:33.251Z",
            "artist_image": []
        }
    ]
}

再次 - 如果我使用 console.log,所有字段都会显示,但不是 image.url

【问题讨论】:

  • 您必须在问题中显示 json

标签: json vue.js nuxt.js strapi


【解决方案1】:

知道了——图像字段是一个数组,因为我允许在 Strapi 上使用多种媒体。不得不做 event.image[0].url

【讨论】:

  • 在某些情况下,做一个 for 循环可能更实用,所以你一定会得到所有的项目。
猜你喜欢
  • 2023-03-05
  • 2021-09-07
  • 2021-05-10
  • 2020-12-30
  • 1970-01-01
  • 2021-07-20
  • 1970-01-01
  • 2019-10-22
  • 2019-12-18
相关资源
最近更新 更多