I have an express app, storing data in mongo, using Jade as the view engine. I have a simple route that gets the docs in a particular collection, each doc corresponding to a product. The image is base64 encoded. When I try and render as an image though it doesn't work
My route is
exports.index = function(req, res){
mongo.getProducts(function(data) {
res.render('consumer/index', {user: req.session.user, products: data});
});
};
The function that calls is
exports.getProducts = function(callback) {
Product.find().exec(function(err, products){
return callback(products);
});
};
and then my Jade file has the following code
each val in products
img(src="data:image/png;base64,'+#{val.image.data}+'", alt='Image', style="width: 20px; height: 20px")
Looking at the doc directly in Mongo (via robomongo) I get this
I don't know what I'm missing, because in another file I use jQuery datatables to show the documents, and the same approach there correctly renders the image, here is a snippet of the datatables code
"aoColumns": [
{"mData": "name"},
{"mData": "price"},
{"mData": "category"},
{"mData": "description"},
{"mData": "image.data", "mRender": function ( data, type, full ) {
return '<img src="data:image/png;base64,'+data+'", style="width: 20px; height: 20px"></>'}},
{"mData": "promoted"},
{"mData": null}
]
val.image.data.$binary
isundefined
– Eshelman