我正在尝试渲染存储在 MongoDB 中的图像,这些图像由我的 api 获取。图像存储为缓冲区。但我无法这样做。我在这上面花了几个小时。请看一看。提前致谢。
回收路线
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>
<% } %>
我遵循了 https://www.geeksforgeeks.org/upload-and-retrieve-image-on-mongodb-using-mongoose/
中给出的方法