I'm having some serious difficulties accessing my images from Strapi, and everything that I've come across on here just says to add 'http://localhost:1337' before accessing the URL but that's not working for me. I'm super confused as to whats going and I also don't understand why it's so difficult to access them in the first place?
// constants
const strapi = 'http://localhost:1337/posts'
const rootNode = document.getElementById('root');
const api_url = "http://localhost:1337"
function generateHTMLtemplate(data) {
// let url = JSON.stringify(data.image.formats.small.url)
// console.log(url)
// ^this returns "Cannot read properties of undefined (reading 'url')"
// console.log(api_url + url)
// ^this also returns "Cannot read properties of undefined (reading 'url')"
const { title, description } = data
return `
<div class="time-card-header">
<header class="time-card-header">
<div class="header-content">
<h4 class="timeline-title"><span class="badge">${title}</span></h4>
<p>${description}</p>
<div class="circle">
<img src=>
</div>
</div>
</header>
<div class="line"></div>
</div>
`
}
// render to the dom
function renderDataToTheDom(node, data) {
const html = data.map(item => generateHTMLtemplate(item)).join('')
console.log(html)
node.innerHTML = html
}
// renderDataToTheDom(root, [1,2,3])
async function getData(url) {
try {
const response = await fetch(url)
const data = await response.json();
renderDataToTheDom(root, data)
} catch (error){
console.log("error", error.message)
}
}
getData(strapi)
So I run into the issue as soon as I try to access the .url
...
I would really appreciate any help on this, I've also tried to use an S3 plugin that I saw some people suggest but that didn't work either and I'd rather keep the solution as simple as possible as the site is a pretty simple one
my json:
{
"id": 1,
"Title": "Santa Dog Picture",
"published_at": "2021-03-29T02:45:32.389Z",
"created_at": "2021-03-29T02:45:23.362Z",
"updated_at": "2021-03-29T02:45:32.414Z",
"Photo": {
"id": 3,
"name": "Pets3.jpg",
"alternativeText": "",
"caption": "",
"width": 4000,
"height": 6000,
"formats": {
"thumbnail": {
"name": "thumbnail_Pets3.jpg",
"hash": "thumbnail_Pets3_a4be530d90",
"ext": ".jpg",
"mime": "image/jpeg",
"width": 104,
"height": 156,
"size": 5.74,
"path": null,
"url": "/uploads/thumbnail_Pets3_a4be530d90.jpg"
},
"large": {
"name": "large_Pets3.jpg",
"hash": "large_Pets3_a4be530d90",
"ext": ".jpg",
"mime": "image/jpeg",
"width": 667,
"height": 1000,
"size": 85.36,
"path": null,
"url": "/uploads/large_Pets3_a4be530d90.jpg"
},
"medium": {
"name": "medium_Pets3.jpg",
"hash": "medium_Pets3_a4be530d90",
"ext": ".jpg",
"mime": "image/jpeg",
"width": 500,
"height": 750,
"size": 56.22,
"path": null,
"url": "/uploads/medium_Pets3_a4be530d90.jpg"
},
"small": {
"name": "small_Pets3.jpg",
"hash": "small_Pets3_a4be530d90",
"ext": ".jpg",
"mime": "image/jpeg",
"width": 333,
"height": 500,
"size": 31.39,
"path": null,
"url": "/uploads/small_Pets3_a4be530d90.jpg"
}
},
"hash": "Pets3_a4be530d90",
"ext": ".jpg",
"mime": "image/jpeg",
"size": 2031.2,
"url": "/uploads/Pets3_a4be530d90.jpg",
"previewUrl": null,
"provider": "local",
"provider_metadata": null,
"created_at": "2021-03-29T02:42:56.325Z",
"updated_at": "2021-03-29T02:42:56.464Z"
}
}