使用 Strapi / Nuxt 获取图片 URL 时出现问题
2020-12-19
1079
我试图从 JSON 中获取图像 URL,该 JSON 是调用 strapi 内容时获取的。它用于活动网站。这是代码。当我 console.log 图像 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
1个回答
明白了——图像字段是一个数组,因为我允许在 Strapi 上使用多个媒体。必须执行 event.image[0].url
sometimes_prog
2020-12-20