如何使用带有Object数组的把手来显示表格

时间:2017-10-18 12:55:48

标签: javascript node.js handlebars.js express-generator

我使用带把手作为模板引擎的快速生成器创建了一个Web应用程序。如果我从响应中显示对象数组,则显示。但是,当我在视图中的每个循环都没有显示任何内容时。

/********************************
model file ../controllers/books.js
*********************************/
var request = require('request');

exports.list = function(req, res, next){

  request.get({ url: "https://jsonplaceholder.typicode.com/posts" },      function(error, response, body) { 
               if (!error && response.statusCode == 200) { 
                    res.render('index', { title: 'speed Tracker', list: body });     
                  } 
              });


};
  /***********************
route file 
************************/
var express = require('express');
var router = express.Router();

var books = require('../controllers/books'); 
/* GET home page. */
router.get('/', books.list);

<!-- language: lang-html -->

                      <table id="datatable" class="table table-striped table-bordered">
                           <thead>
                               <tr>
                                   <th>ID</th>
                                   <th>Album Id</th>
                                   <th>title</th>
                                   <th>url</th>
                                   <th>image</th>
                               </tr>
                           </thead>
                          {{list}}
                           <tbody>
                              {{#each list}}
                               <tr>
                                   <td>{{id}}</td>
                                   <td>{{userId}}</td>
                                   <td>{{title}}</td>
                                   <td>{{body}}</td>
                                   <td></td>
                               </tr>
                               {{/each}}
                           </tbody>
                       </table>
json of list

[   {     &#34; userId&#34;:1,     &#34; id&#34;:1,     &#34; title&#34;:&#34; sunt autrerere repellat provident occaecati excepturi optio reprehenderit&#34;,     &#34; body&#34;:&#34; quia et suscipit \ nsuscipit recusandae consequuntur expedita et cum \ nreprehenderit molestiae ut ut quas totam \ nnostrum rerum est autem sunt rem eveniet architecto&#34;   },   {     &#34; userId&#34;:1,     &#34; id&#34;:2,     &#34; title&#34;:&#34; qui est esse&#34;,     &#34; body&#34;:&#34; est rerum tempore vitae \ nsequi sint nihil reprehenderit dolor beatae ea dolores neque \ nfugiat blanditiis voluptate porro vel nihil molestiae ut reiciendis \ nqui aperiam non debitis possimus qui neque nisi nulla&#34;   },   {.....

我添加了上面的代码,每个循环都没有显示数据。

1 个答案:

答案 0 :(得分:0)

我必须在book.js文件中将列表从String转换为Object。我在下面添加了代码。

var request = require('request');

exports.list = function(req, res, next){

  request.get({ url: "https://jsonplaceholder.typicode.com/posts" },      function(error, response, body) { 
               if (!error && response.statusCode == 200) { 
                    res.render('index', { title: 'speed Tracker', list: JSON.parse(body) });     // add JSON.parse to convert string to object :)
                  } 
              });


};