Tuesday, 29 October 2019

How Could I Write a helper function which is query from mongodb , and then can call this func for any ejs template

I want to write a function with one param id for this mongo to pass to my home.ejs.

Example with this query:

db.getCollection('users').find('5da85558886aee13e4e7f044', {image: 1})

Example I have a route.

routes/users.js

var User = require("../models/user");
var db = require("../secure/db");
var middleware = require("../middlewares/middleware");
 //...
router.get("/home", middleware.isAllowed, function(req, res, next) {
 res.render('home',{
  title: "Project",
  userList: userList,
  getUserimage: getUserimage(??)//trying here 
 })

});

so in home.ejs or any file.ejs I can call like getUserimage('5da85558886aee13e4e7f044') inside loop of list of ids

views/home.ejs

 <% layout('layout/layout') %>
 //more code
 <% for(var i=0; i < userList.length; i++){ %>
    <div>
    <img src ="<%=getUserimage(userList[i].id)%>" > <%=userList[i].name%> 
    </div>
 <% } %>
 //more code


from How Could I Write a helper function which is query from mongodb , and then can call this func for any ejs template

No comments:

Post a Comment