Search code examples
node.jsmongodbpug

Render image from mongo atlas with pug


I am a beginner learning nodejs and I would like to know the right way to retrieve or render an image from mongo atlas using pug and multer because I am stuck.

const storage=multer.diskStorage({
   destination:function(req,file,cb){
     cb(null,"images");
   },
   filename:function(req,file,cb){
     cb(null,"-"+Date.now()+file.originalname);
   }   
})

const upload=multer({storage:storage});

I have the destination folder as images with the pug img src as

img(src="/images/#{result.pic}").
app.get("/",function(req,res){ 
  user.find().then(function(result){
  res.render("home",{results:result})
})})

but does not seem to work


Solution

  • As noted in the Pug documentation, the #{foo} interpolation syntax is no longer supported in attributes.

    Instead of using—

    img(src='/images/#{result.pic}') // no longer supported
    

    —you can use:

    img(src='/images/' + result.pic) // supported
    

    Or, if you have ES6 support, you can use template strings:

    img(src=`/images/${result.pic}`) // supported