[英]Displaying data from a Mongodb collection in ejs using Mongoose
我是編程的新手。 我的本地數據庫中有一個名為“Practice”的集合,其中包含“名稱、角色、組織”。 我想弄清楚如何使用貓鼬在 .ejs 文件中打印此信息。
在我的 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); });
在路線中,
app.get('/profileface', isLoggedIn, function(req, res) {
res.render('profileface.ejs', {
user : req.user
});
});
在視圖文件夾中的文件 profileface.ejs 中,我有以下內容可以打印我的“練習”集合中的名稱。
<%= practice.name %>
雖然它在控制台中打印,但當我嘗試訪問 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)
在過去的 2 天里,我一直試圖通過谷歌搜索來解決這個問題,但現在我放棄了。 如果您能幫助我,我將不勝感激。
額外在編程中,您想使代碼井井有條。
在app文件夾中創建一個文件,例如PracticeModel.js,然后在其中移動模式邏輯
var PracticeSchema = new mongoose.Schema({
Name: String,
Role: String,
Org: String
});
module.exports = mongoose.model('practice', PracticeSchema, 'practice');
在routes.js中 ,在頂部包括新創建的文件
var PracticeModel = require('./PracticeModel.js');
您的問題您需要(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
});
});
});
在您的個人資料profileface.ejs中 ,您需要遍歷傳遞的實踐數組
<% practices.forEach(function (practice) { %>
<%= practice.Name %> <!-- note you defined the field as Name not name -->
<% }) %>
更改后,您的server.js將如下所示
mongoose.connect(configDB.url);
require('./app/routes.js')(app, passport);
閱讀有關同步與異步的文章 。 您在Node.js中所做的大多數事情通常都是異步的。
嗨,我遇到了同樣的問題,所以我做了這個並且它奏效了
collection(users).find({}, (err, found) => {
if(err){
console.log(err)
}else{
res.render("Dashboard", {DataFound: found})
}
})
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.