数据未显示nodejs把手

时间:2018-03-01 11:39:44

标签: node.js handlebars.js

我正在尝试显示来自我的数据库的数据,但它似乎没有显示出来,我所做的是在ejs上遵循这个教程,它似乎工作正常,我现在在把手上尝试

app.get('/', function(req, res, next) { 
  req.db.collection('users').find().sort({"_id": -1}).toArray(function(err, result) {
    if (err) {
      req.flash('error', err);
      res.render('user/list', {
        title: 'Users List', 
        data: ''
      });
    } else {
      res.render('user/list', {
        title: 'Users List', 
        data: result
      });
    }
  });
});
<table class="table table-striped">
<tr>
  <th>Name</th>
  <th>Type</th>
  <th>Desciption</th>
  <th>Action</th>
</tr>
{{#if user}}{{#each user}}
<tr>
  <td>{{this.Name}}</td>
  <td>{{this.Type}}</td>
  <td>{{this.Description}}</td>
  <td>
    <div style="float:left">
      <a href='/users/edit/{{ user._id}}'>Edit</a> &nbsp;             
      <form method="post" action="/users/delete/{{user._id}}" style="float:right">
        <input type="submit" name="delete" value='Delete' onClick="return confirm('Are you sure you want to delete?')" />
        <input type="hidden" name="_method" value="DELETE" />
      </form>
    </div>
  </td>
</tr>
{{/each}}{{/if}}
</table>

这是我按照教程制作的ejs项目,目前正在运行

<table width='80%' border=0>

<tr style='text-align:left; background-color:#CCC'>
  <th>Name</th>
  <th>Type</th>
  <th>Description</th>
  <th>Action</th>
</tr>

<!--
  Using FOREACH LOOP for the users array

  myArray.forEach(function(el, index) {
    // el - current element, i - index
  });
-->
<% if (data) { %>
  <% data.forEach(function(user){ %>
    <tr>
      <td><%= user.Name %></td>
      <td><%= user.Type %></td>
      <td><%= user.Description %></td>
      <td>
        <div style="float:left">
          <a href='/users/edit/<%= user._id %>'>Edit</a> &nbsp;             
          <form method="post" action="/users/delete/<%= user._id %>" style="float:right">
            <input type="submit" name="delete" value='Delete' onClick="return confirm('Are you sure you want to delete?')" />
            <input type="hidden" name="_method" value="DELETE" />
          </form>
        </div>
      </td>
    </tr>
  <% }) %>
<% } %>
</table>
app.get('/', function(req, res, next) { 
  // fetch and sort users collection by id in descending order
  req.db.collection('benefits').find().sort({"_id": -1}).toArray(function(err, result) {

    if (err) {
      req.flash('error', err);
      res.render('user/list', {
        title: 'User List', 
        data: ''
      });
    } else {

      res.render('user/list', {
        title: 'User List', 
        data: result
      });
    }
  });
});

我只是尝试将ejs代码转换为处理条形图,并对其工作方式进行了一些研究,但它并没有结束我的方式

我仍然是使用nodejs的初学者,所以请放轻松我

1 个答案:

答案 0 :(得分:0)

您不仅需要将模板从ejs转换为车把,而且还必须使用带有快递的车把中间件。

在这里你会找到一个:https://www.npmjs.com/package/express-handlebars

安装它,例如npm:npm install --save express-handlebars

然后,在您的服务器文件中声明它:

var exphbs  = require('express-handlebars');

var app = express();

修改

我看到您使用某种“局部”来呈现用户的列表页面,因此您必须声明其路径:

var hbs = exbphbs.create({
  defaultLayout: 'main',
  partialsDir: 'views/partials/',
});

app.engine('handlebars', hbs.engine);
app.set('view engine', 'handlebars');

现在您的工作区应如下所示:

app.js
views/
  list.handlebars
  layouts/
    main.handlebars
  partials/
    user/
      list.handlebars

在模板中,检查是否存在user var,因此必须在渲染函数中命名,而不是data:。另外,渲染文件list.handlebars

app.get('/', function(req, res, next) {
  req.db.collection('users').find().sort({"_id": -1}).toArray(function(err, result) {
    if (err) {
      req.flash('error', err);
      res.render('list', {
        title: 'Users List', 
        user: '',
      });
    } else {
      res.render('list', {
        title: 'Users List', 
        user: result,
      });
    }
  });
});

views / list.handlebars:

{{> user/list}}

<强>视图/布局/ main.handlebars

<!DOCTYPE html>
<html>
  <head>
  </head>
  <body>
{{body}}
  </body>
</html>

views / partials / user / list.handlebars :请注意我已移除this.user.

<h1>{{title}}</h1>
<table class="table table-striped">
  <tr>
    <th>Name</th>
    <th>Type</th>
    <th>Desciption</th>
    <th>Action</th>
  </tr>
  {{#if user}}
    {{#each user}}
      <tr>
        <td>{{Name}}</td>
        <td>{{Type}}</td>
        <td>{{Description}}</td>
        <td>
          <div style="float:left">
            <a href='/users/edit/{{ _id}}'>Edit</a> &nbsp;
            <form method="post" action="/users/delete/{{_id}}" style="float:right">
              <input type="submit" name="delete" value='Delete' onClick="return confirm('Are you sure you want to delete?')" />
              <input type="hidden" name="_method" value="DELETE" />
            </form>
          </div>
        </td>
      </tr>
    {{/each}}
  {{/if}}
</table>