如何从mongodb检索数据并在ejs页面中显示? [英] How to retrieve data from mongodb and display in ejs page?
问题描述
我已经将数据添加到mongodb中.我在用猫鼬.我想从mongodb数据库获取数据并将其显示在ejs页面中.我在互联网上搜索了很多东西,但是尝试这些时却遇到了错误.我不明白错误是什么.这是我最近尝试过的.
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.
猫鼬模式
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);
我从mongodb获取数据的地方
Where I get data from mongodb
router.get('/', function(req, res){
user.find({}, function(err, profile){
res.render('AdminDatabase', { profile : profile});
});
});
我在ejs页面中显示的位置
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>
但是当我刷新AdminDatabase页面时,出现错误提示
But When I refresh the AdminDatabase page I get the error saying
ReferenceError:E:/软件项目/Project/myProject/views/AdminDatabase.ejs:67
ReferenceError: E:/Software project/Project/myProject/views/AdminDatabase.ejs:67
配置文件未定义
推荐答案
用户"不会变成用户",因此查找代码应类似于
Doesn't 'User' get turned into 'users', so the find code should look like
users.find({} ...
users.find({}...
..?
这篇关于如何从mongodb检索数据并在ejs页面中显示?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!