2

I have already added data to mongodb. I am using mongoose. I want to get data from mongodb database and display that in ejs page. I have searched a lot in the internet but I am getting errors while trying those. I do not understand what the error is. This is what I recently tried.

Mongoose schema

 var mongoose = require("mongoose");

 var Schema = mongoose.Schema;

 var UserSchema = new Schema({

     username:String,
     _id:String,
     dateOfBirth:Date,
     telephoneNo:Number,
     address:String,
     email:String,
     fb_Id:String,
     jobTitle:String,
     password:String
 });

 module.exports=mongoose.model('User',UserSchema);

Where I get data from mongodb

router.get('/', function(req, res){
user.find({}, function(err, profile){
    res.render('AdminDatabase', { profile : profile});
   });
});

Where I display in the ejs page

<div>
    <ul>
    <% for(var i=0; i<profile.length; i++) {%>
        <li><%= profile[i].username %></li>
        <li><%= profile[i]._id %></li>
        <li><%= profile[i].dateOfBirth %></li>
        <li><%= profile[i].telephoneNo %></li>
        <li><%= profile[i].address %></li>
        <li><%= profile[i].email %></li>
        <li><%= profile[i].fb_Id %></li>
        <li><%= profile[i].jobTitle %></li>
    <% } %>
    </ul>
</div>

But When I refresh the AdminDatabase page I get the error saying

ReferenceError: E:/Software project/Project/myProject/views/AdminDatabase.ejs:67

profile is not defined

Kabilesh
  • 1,000
  • 6
  • 22
  • 47

1 Answers1

-1

Doesn't 'User' get turned into 'users', so the find code should look like

users.find({}...

..?

Omar Shishani
  • 379
  • 3
  • 8
  • 1
    Please provide a formatted code answer with an explanation. SO doesn't work like most forums where you can leave a comment to kick off a discussion and eventually get to the answer, it expects solid/complete answers to questions. – gilliduck Mar 22 '20 at 16:48