Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Resolved promise but unable to insert data into react project

  • 23-10-2024 09:55PM
    #1
    Registered Users, Registered Users 2 Posts: 4,687 ✭✭✭


    Hi guys,

    Using contentful as my CMS and retrieving my content using code below

    const ProductsGrid = () => {   

    let imageURL;
        const client = createClient({       

    client.getEntries().then(function (entries) {       

    entries.items.forEach(function (entry) {     

    imageURL = 'http://' + entry.fields.image.fields.file.url           

    console.log(imageURL)       

    } )   

    })

    In the console I can see I am retrieving my images successfully from my CMS

    http:////images.ctfassets.net/zpu537igy6hd/10jOjKpdTYAYRVc197KlAs/60910da04dceab7acda47c042261776e/397526804_263713576623932_2944288645324919698_n.jpg
    ProductsGrid.jsx:17

    http:////images.ctfassets.net/zpu537igy6hd/2fat5G5gnOIT00YAdoSVZg/ea3f8c44b62cf977bc8a0d5b3e4eeea7/397492507_267421149586508_6988846565487064728_n.jpg

    But when I try to render these images they are not been displayed, when I click on img tag I get undefined

    return (

    <figure className='px-4 pt-4'>                           

    <img  src={imageURL} alt={title}  className='rounded-xl h-64 md:h-48 w-full object-cover'  />                       

    </figure>

    If I copy the links from my console directly into my img tag above they work, but when I try render them from imageURL they fail.

    Think it may be how I'm declaring imageURL at top of script and this is then been rendered by react before promise has resolved.

    But unsure how else to get imageURL in scope to to render in img tag.

    Any help appreciated.



Comments

  • Registered Users, Registered Users 2 Posts: 6,613 ✭✭✭daymobrew


    It looks like the 'http://' part is not needed.

    Try change:

    imageURL = 'http://' + entry.fields.image.fields.file.url

    to

    imageURL = entry.fields.image.fields.file.url



Advertisement