node.js - 我怎样才能让 ejs 与 mongoose 一起工作?

标签 node.js mongodb express mongoose ejs

我有一条获取所有客户端的路线,我正在尝试将客户端传递给 ejs 来渲染它们。我收到以下错误:

Express
500 SyntaxError: Unexpected token {
at Object.Function (unknown source)
at exports.compile (/Users/sm/Desktop/AttApp/node_modules/ejs/lib/ejs.js:234:12)
at Object.exports.render (/Users/sm/Desktop/AttApp/node_modules/ejs/lib/ejs.js:273:10)
at View.exports.renderFile [as engine] (/Users/sm/Desktop/AttApp/node_modules/ejs/lib/ejs.js:303:22)
at View.render (/Users/sm/Desktop/AttApp/node_modules/express/lib/view.js:75:8)
at Function.app.render (/Users/sm/Desktop/AttApp/node_modules/express/lib/application.js:503:10)
at ServerResponse.res.render [as partial] (/Users/sm/Desktop/AttApp/node_modules/express/lib/response.js:721:7)
at ServerResponse.module.exports.res.render (/Users/sm/Desktop/AttApp/node_modules/express-partials/index.js:55:9)
at ServerResponse.res.renderPjax (/Users/sm/Desktop/AttApp/node_modules/express-pjax/pjax.js:17:11)
at Promise. (/Users/sm/Desktop/AttApp/app.js:61:17)

这是我的路线:

app.get( '/clients', function( req, res ) {
    return ClientModel.find( function( err, clients ) {
        if( !err ) {
            res.renderPjax('clients/clients.ejs', {
              title: 'Clients Page',
              clients: clients

            });

            console.log(clients);
        } else {
            return console.log( err );
        }
    });
});

这是我的 View client.ejs:

<input type="text" class="table-search" id="search" autocomplete="off" placeholder="Search Clients…">
<table class="table" id="tblData">
        <thead>
        <tr>
        <th>Client Name</th>
        <th>Title</th>
        </tr>
        </thead>
            <tbody id="tblDataBody">
            <% clients.forEach(fucntion(client){ %>

                <tr>
                    <td><a href="http://lar4.loc/clients/<%= client._id %>"><%= client.first_name %></a></td>
                    <td>Title</td>
                </tr>

            <%  }) %>

            </tbody>
</table>

如何让 ejs 模板循环遍历客户端数据并渲染它?我正在使用 Mongoose 来存储和获取数据。我也在使用express.js。

最佳答案

我认为这是由于该行中的错误

<% clients.forEach(fucntion(client){ %>

由于您拼写错误function,它无法解释{

关于node.js - 我怎样才能让 ejs 与 mongoose 一起工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15843439/

相关文章:

mongodb - 无法创建/访问数据库

node.js - 返回类中的中间件

javascript - 如何排除特定版本的 npm 包?

Javascript lodash 展平分层对象

javascript - 如何在 Node js中的同一模块文件中使用模块

windows - Nodejs - Windows key /证书存储

javascript - 如何使用 Mongoose.js 3.8.20 和 MongoDB 2.6.5 对文档进行排序?

mongodb - 如何在mongodb数组中获取匹配的文档?

javascript - 我应该如何在 Node Express 应用程序中将 Parse 用户身份验证作为中间件实现?

node.js - 更改后重新处理请求