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