使用Mongoose在ejs中显示Mongodb集合中的数据

时间:2017-10-04 12:45:05

标签: node.js mongodb mongoose ejs

我是编程的新手。我有一个名为" Practice"在我的本地数据库中有" name,role,org"。我试图弄清楚如何使用mongoose在.ejs文件中打印此信息。

在我的server.js中,我有

app.get('/profileface', isLoggedIn, function(req, res) {
        res.render('profileface.ejs', {
            user : req.user
        });
    });

在路线中,

<%= practice.name %>

在views文件夹中,文件profileface.ejs,我在下面打印了我的&#34;练习&#34;集合。

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)

虽然它是在控制台中打印,但当我尝试访问profileface.ejs时,我收到以下错误。

{{1}}

我花了最近两天试图通过谷歌搜索来解决这个问题,但现在我放弃了。如果你能帮助我,我将非常感激。

1 个答案:

答案 0 :(得分:1)

额外在编程中,您希望保持代码的有序性。

创建一个文件,例如在 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);     

synchronous vs asynchronous上阅读此文章。您在Node.js中执行的大多数操作通常都是异步的。