[英]Displaying data from a Mongodb collection in ejs using Mongoose
I am brand new to programming.我是编程的新手。 I have a collection called "Practice" in my local database which has "name, role, org".
我的本地数据库中有一个名为“Practice”的集合,其中包含“名称、角色、组织”。 I am trying to figure out how to print this info in a .ejs file using mongoose.
我想弄清楚如何使用猫鼬在 .ejs 文件中打印此信息。
In my server.js, I have在我的 server.js 中,我有
require('./app/routes.js')(app, passport);
mongoose.connect(configDB.url); // connect to our database
var schema = mongoose.Schema;
mongoose.model('practice', new schema({ Name: String, Role: String, Org: String}),'practice');
var practice = mongoose.model('practice');
practice.find({}, function(err, data) { console.log(err, data); });
In the routes,在路线中,
app.get('/profileface', isLoggedIn, function(req, res) {
res.render('profileface.ejs', {
user : req.user
});
});
In the views folder, file profileface.ejs, I have the below to print the name from my "practice" collection.在视图文件夹中的文件 profileface.ejs 中,我有以下内容可以打印我的“练习”集合中的名称。
<%= practice.name %>
Although it is printing in the console, when I try to access profileface.ejs, I get the following error.虽然它在控制台中打印,但当我尝试访问 profileface.ejs 时,出现以下错误。
ReferenceError: C:\Users\SEESCHU\Desktop\Linneus university\Assignments\302\Assignment 2\code\Test3\views\profileface.ejs:36 34| </div> 35| >> 36| <%= practice.name %> 37| 38| <!-- <div class="text-center"> 39| <p>Assignment for 4ME302</p> practice is not defined at eval (eval at <anonymous> (C:\Users\SEESCHU\Desktop\Linneus university\Assignments\302\Assignment 2\code\Test3\node_modules\ejs\lib\ejs.js:237:14), <anonymous>:30:986) at eval (eval at <anonymous> (C:\Users\SEESCHU\Desktop\Linneus university\Assignments\302\Assignment 2\code\Test3\node_modules\ejs\lib\ejs.js:237:14), <anonymous>:30:1154) at C:\Users\SEESCHU\Desktop\Linneus university\Assignments\302\Assignment 2\code\Test3\node_modules\ejs\lib\ejs.js:250:15 at Object.exports.render (C:\Users\SEESCHU\Desktop\Linneus university\Assignments\302\Assignment 2\code\Test3\node_modules\ejs\lib\ejs.js:288:13) at View.exports.renderFile [as engine] (C:\Users\SEESCHU\Desktop\Linneus university\Assignments\302\Assignment 2\code\Test3\node_modules\ejs\lib\ejs.js:318:20) at View.render (C:\Users\SEESCHU\Desktop\Linneus university\Assignments\302\Assignment 2\code\Test3\node_modules\express\lib\view.js:76:8) at Function.app.render (C:\Users\SEESCHU\Desktop\Linneus university\Assignments\302\Assignment 2\code\Test3\node_modules\express\lib\application.js:504:10) at ServerResponse.res.render (C:\Users\SEESCHU\Desktop\Linneus university\Assignments\302\Assignment 2\code\Test3\node_modules\express\lib\response.js:798:7) at C:\Users\SEESCHU\Desktop\Linneus university\Assignments\302\Assignment 2\code\Test3\app\routes.js:30:7 at callbacks (C:\Users\SEESCHU\Desktop\Linneus university\Assignments\302\Assignment 2\code\Test3\node_modules\express\lib\router\index.js:164:37) at isLoggedIn (C:\Users\SEESCHU\Desktop\Linneus university\Assignments\302\Assignment 2\code\Test3\app\routes.js:116:10) at callbacks (C:\Users\SEESCHU\Desktop\Linneus university\Assignments\302\Assignment 2\code\Test3\node_modules\express\lib\router\index.js:164:37) at param (C:\Users\SEESCHU\Desktop\Linneus university\Assignments\302\Assignment 2\code\Test3\node_modules\express\lib\router\index.js:138:11) at pass (C:\Users\SEESCHU\Desktop\Linneus university\Assignments\302\Assignment 2\code\Test3\node_modules\express\lib\router\index.js:145:5) at Router._dispatch (C:\Users\SEESCHU\Desktop\Linneus university\Assignments\302\Assignment 2\code\Test3\node_modules\express\lib\router\index.js:173:5) at Object.router (C:\Users\SEESCHU\Desktop\Linneus university\Assignments\302\Assignment 2\code\Test3\node_modules\express\lib\router\index.js:33:10)
I have spent the last 2 days trying to figure it out by googling but now I give up.在过去的 2 天里,我一直试图通过谷歌搜索来解决这个问题,但现在我放弃了。 I would really appreciate if you can help me.
如果您能帮助我,我将不胜感激。
Extra In programming, you want to keep your code organized. 额外在编程中,您想使代码井井有条。
Create a file eg PracticeModel.js inside app folder and move your schema logic there 在app文件夹中创建一个文件,例如PracticeModel.js,然后在其中移动模式逻辑
var PracticeSchema = new mongoose.Schema({
Name: String,
Role: String,
Org: String
});
module.exports = mongoose.model('practice', PracticeSchema, 'practice');
In your routes.js , include the newly created file at the top 在routes.js中 ,在顶部包括新创建的文件
var PracticeModel = require('./PracticeModel.js');
Your problem You need to (1) move the query inside your route handler and (2) pass the resultset data
to the view 您的问题您需要(1)在路由处理程序内移动查询,并且(2)将结果集
data
传递给视图
app.get('/profileface', isLoggedIn, function(req, res) {
// mongoose operations are asynchronous, so you need to wait
PracticeModel.find({}, function(err, data) {
// note that data is an array of objects, not a single object!
res.render('profileface.ejs', {
user : req.user,
practices: data
});
});
});
In your view profileface.ejs , you need to iterate over the passed practices array 在您的个人资料profileface.ejs中 ,您需要遍历传递的实践数组
<% practices.forEach(function (practice) { %>
<%= practice.Name %> <!-- note you defined the field as Name not name -->
<% }) %>
Your server.js would look like this after the changes 更改后,您的server.js将如下所示
mongoose.connect(configDB.url);
require('./app/routes.js')(app, passport);
Read this post on synchronous vs asynchronous . 阅读有关同步与异步的文章 。 Most of the things you do in Node.js is usually asynchronous.
您在Node.js中所做的大多数事情通常都是异步的。
Hi I had the same problem, So I Did This and it worked嗨,我遇到了同样的问题,所以我做了这个并且它奏效了
collection(users).find({}, (err, found) => {
if(err){
console.log(err)
}else{
res.render("Dashboard", {DataFound: found})
}
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.