Home > Net >  How to display an html string stored in the mongodb using express
How to display an html string stored in the mongodb using express

Time:02-03

I've been stuck for displaying or rendering an html string coming from the db..So any help is appreciated.

This is the rendering controller for rendering the data.

exports.previewPageView = (req, res, next) => {
    // We Will See This Part
    pageDataModel.findOne({url : url}).populate("page_id").exec(function(err, result){
        if(err) throw err;
        else {
            if(result == null)
            {
                res.status = 404;
                next();
            }
            else
            {
                var obj = {};
                for(let i = 0; i < result.pageData.length; i  )
                {
                    if(result.pageData[i].imagePath == undefined)
                    {
                        let name = result.pageData[i].fieldRef;
                        obj[name] = ejs.render(result.pageData[i].fieldValue, {category : req.params.category});
                    }
                    else
                    {
                        let name = result.pageData[i].fieldRef;
                        obj[name] ={src : result.pageData[i].imagePath, alt : ejs.render(result.pageData[i].fieldValue, {category : req.params.category})} 
                    }
                    
                }

                
                res.render(`${path.join(process.cwd()   "/views"   result.page_id.templatePath)}`, {data : obj});
            }
        }
    });
}

and this is the output in html... This is the output image of the page

CodePudding user response:

If We want to sent html data string directly as variable to the view we can use the <%-variable%> syntax which concludes unescaped raw output with <%- %> in ejs.

  •  Tags:  
  • Related