【问题标题】:How can I get ejs to work with mongoose?我怎样才能让 ejs 与猫鼬一起工作?
【发布时间】:2013-03-28 10:19:07
【问题描述】:

我有一条获取所有客户端的路由,我正在尝试将客户端传递给 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 );
        }
    });
});

这是我的视图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 模板循环访问客户端数据并呈现它?我正在使用猫鼬来存储和获取数据。我也在使用 express.js。

【问题讨论】:

    标签: node.js mongodb express mongoose ejs


    【解决方案1】:

    我认为是由于这一行的错误

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-12-24
      • 2014-10-21
      • 1970-01-01
      • 2013-08-24
      • 2013-03-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多