Skip to content Skip to sidebar Skip to footer

Fetching Data From Mongodb Through Nodejs And Express To A Html Page

Basically, I want to show particular fields from 'employees' collection into a html page. But even after searching a lot on web, I'm unable to do so. Here is the route part from th

Solution 1:

Using ejs, you need to set the view engine:

app.set('view engine', 'ejs');

Then get your data:

 app.get('/employees',(req , res) =>{
    db.collection('employees').find().toArray(function(err , i){
        if (err) returnconsole.log(err)

        res.render('index.ejs',{employees: i})  
     })
 });

The .ejs file would be like this:

employees
  <ul class="employees">
  <% for(var i=0; i<employees.length; i++) {%>
    <liclass="employees"><span><%= " Nome: " +employees[i].name+"."%></span><span><%=" Address: " + employees[i].address%></span></li>
  <% } %>
</ul>

Just a simple way using ejs. Hope it helps to clarify things.

Post a Comment for "Fetching Data From Mongodb Through Nodejs And Express To A Html Page"