I want print one list in my pug template, so i receive a json and render into my page, that way:
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;
and my template:
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));
})
}
and in my shell i receive this:
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 } ]
but return this message: Cannot read property 'length' of undefined