如何使用 EJS 呈现使用 Express 和 Node.JS 的 Mongodb 查询的结果?

How to render with EJS the result of a Mongodb query using Express and Node.JS?

我试图避免每次使用时都创建数据库查询 router.get("/whatever",function()...)

所以我创建了一个 dbController,其中 returns 一个文档数组
db.collection.find().toArray()

工作正常。 虽然我在这里窒息

router.get( "/test" , function ( req , res , err ) {
        if (err) console.dir( err );

    var categoriesArray = require ( "./Controllers/dbController" ).getCategsArray();

    console.log( "!!!!!!!!!!!!!!!" + categoriesArray );

    res.render( "hello" , {
                _: _ ,
            title: "la naiba" ,
            items: categoriesArray
        });
    });

因为虽然 console.log 显示文档数组

[Function: next]
!!!!!!!!!!!!!!!
[ { _id: ObjectID { _bsontype: 'ObjectID', id: 'QrÑUÿY?ó#M_?' },
    categories: [ [Object], [Object] ],
    id: 'mens',
    name: 'Mens',
    page_description: 
................................

我没有在 EJS 模板中获取它

..............................................
<% _.each(items, function(topC) { %>
<li>
    <h1><%= topC.name %></h1>
..........................

title property 虽然得到渲染。非常感谢任何帮助,因为我是一个完整的 n00b,我整晚都在努力取得进步


要交互的名称和列表在categoriesArray里面,那么你需要交互categoriesArray。像这样:

<% items.forEach(function (topC) { %>
    <li>  
        <h1><%= topC.name %></h1>  
    </li>                                   
<% }) %>