Unable to render a buffer png

Viewed 47

I am trying to render images stored in MongoDB which are being fetched by my api. The image is stored as buffer. But I am unable to do so. I have spent hours on this. Please take a look into it. Thanks in advance.

Route for retreival

const axios = require('axios')
exports.homeRoutes = (req,res) => {
    axios.get('http://localhost:5000/api/therapists')
        .then(function(response){
            res.render('index', { therapists : response.data });
        })
        .catch(err =>{
            res.send(err);
        })
}

EJS

<% for(var i = 0; i < therapists.length; i++) { %>
    <tr>
        <td><%= i + 1 %></td>
        <td><img src="data:image/<%=therapists[i].image.contentType%>;base64,
            <%=therapists[i].image.data.toString('base64')%>" /></td>
        <td><%=therapists[i].image.data%></td>
        <td><%= therapists[i].name %></td>
        <td><%= therapists[i].title %></td>
        <td><%= therapists[i].qualification %></td>
        <td><%= therapists[i].experience %></td>
        <td>
        </td>
    </tr>
<% } %>

I followed the approach given at https://www.geeksforgeeks.org/upload-and-retrieve-image-on-mongodb-using-mongoose/

0 Answers
Related