node.js - 使用 Mongoose 在 ejs 中显示 Mongodb 集合中的数据

标签 node.js mongodb mongoose ejs

我是编程新手。我的本地数据库中有一个名为“Practice”的集合,其中包含“名称、角色、组织”。我正在尝试弄清楚如何使用 mongoose 在 .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); });

在 route ,

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

在 View 文件夹中,文件 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)

过去两天我试图通过谷歌搜索来找出答案,但现在我放弃了。如果您能帮助我,我将非常感激。

最佳答案

额外在编程中,您希望保持代码井井有条。

创建一个文件,例如PracticeModel.js 位于 app 文件夹中,并将架构逻辑移至此处

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 传递到 View

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
        });
    });
});

在您的 View profileface.ejs中,您需要迭代传递的practices数组

<% 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 中执行的大部分操作通常都是异步的。

关于node.js - 使用 Mongoose 在 ejs 中显示 Mongodb 集合中的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46565239/

相关文章:

angularjs - 是否可以使用带有表单搜索查询输入文本的 Angular Controller 通过 Mongoose 在 MongoDB 中进行实时搜索?

node.js - 在 Node js中从树数组构建平面数组

node.js - 如何从 "sh"shell(在 Nodejs 上)激活 Conda 环境?

node.js - 如何用node jsexpress响应xml?

arrays - 将数组中元素的字段与 MongoDB 中的字段进行比较

MongoDB - 通过数组中多个对象的相同字段查询文档

node.js - 使用 Multer 获取图片尺寸

node.js - Nodejs 中的 Promisify 事件处理程序和超时

javascript - 如何删除 Mongoose 中的特定子文档

Javascript 存储为 mongoDB 的变量逗号分隔字符串