在nodejs中使用Json对象渲染哈巴狗模板

时间:2017-07-26 04:00:11

标签: json node.js mongodb express

我想在我的哈巴狗模板中打印一个列表,所以我收到一个json并渲染到我的页面中,这样:

var express = require('express');
var router = express.Router();
const dbIn = require('../dbInterations/functions')


router.get('/', function(req, res, next) {
games = dbIn.findAll(); 
/*this function returns a json like this:   { 
_id: 5977ff9052dd664d88e45164, title: 'fallout 4',  imagePath: 
'../public/images/fallout 4.jpg', __v: 0 } ]
 */
 console.log(games);
res.render('index',
{ title: 'Express',
games: games,
           });
});

module.exports = router;

和我的模板:

ul
 each value in games
   p #{value.title}

function dbIn:

 exports.findAll = function(){
 Game.find().lean().exec(function (err, games) {
 if (err) return console.error(err);
 console.log(games);
 return (JSON.stringify(games));
  })
 }

在我的shell中我收到了:

  GET / 500 797.182 ms - 2550
     [ { _id: 5977ff7252dd664d88e45163,
  title: 'opa',
  imagePath: '../public/images/opa.jpg',
  __v: 0 },
  { _id: 5977ff9052dd664d88e45164,
  title: 'fallout 4',
  imagePath: '../public/images/fallout 4.jpg',
  __v: 0 } ]

但返回此消息:无法读取未定义的属性“长度”

0 个答案:

没有答案