Search code examples
reactjsmulter

why is this not rendering my image? Even when the src is correct


I'm trying to build a image uploader with multer and React, I've done this correctly, as the images upload correctly to the database. The problem is when I try to render the images in the src. As you can see in here, I've tried to render the images by setting this code:

I've set to the folder trainfitbackend/uploads/name of the image as it is where I have the images stored but they are not rendering.

{`../trainfitbackend/uploads/${client.image}`}

Here is the full code:

import React, {useState, useEffect} from 'react'
import axios from 'axios'
import {
    BrowserRouter as Router,
    Switch,
    Route,
    Link
  } from "react-router-dom";
import ClientForm from './clientform.js'

function Clients() {

    const [data, setData] = useState([])

    useEffect(() => {
        axios.get('http://localhost:4000/clients')
        .then(res => setData(res.data))
    }, [])
    console.log(data)
    return (
        <div>
            <Link to="/addClient"><button class="bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded">New Client</button></Link>
            <div class="min-h-screen bg-gray-100 flex flex-row">
            <div class="container flex flex-row">
        {data.map((client) => {
                return (
                      <div class="max-w-sm py-32">
                        <div class="bg-white relative shadow-lg hover:shadow-xl transition duration-500 rounded-lg">
                          <img class="rounded-t-lg" src={`../trainfitbackend/uploads/${client.image}`} alt="" /> 
                          <div class="py-6 px-8 rounded-lg bg-white">
                            <h1 class="text-gray-700 font-bold text-2xl mb-3 hover:text-gray-900 hover:cursor-pointer">{client.firstName}</h1>
                            <p class="text-gray-700 tracking-wide">{client.lastName}</p>
                            <Link to={`Clients/${client._id}`}><button class="mt-6 py-2 px-4 bg-yellow-400 text-gray-800 font-bold rounded-lg shadow-md hover:shadow-lg transition duration-300">See more details</button></Link>
                          </div>
                        </div>
                      </div>  
                )
            })}
               </div>   
              </div>
        </div>
    )
}

export default Clients

Solution

  • It was resolved during a discussion in the chat How to fetch images from node.js server's folder in URL?

    Images was moved to public folder