0

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

  • what does in `dbIn.findAll()`. is it a database reading function? – Viran Malaka Jul 26 '17 at 04:07
  • At a stab `games = dbIn.findAll()` likely simply does not return what you are expecting it to. The underlying database methods are "async" and therefore should require some callback or promise resolution. This is your own custom method, not included here. But it's not likely you are handling the "async" calls in the correct way. – Neil Lunn Jul 26 '17 at 04:07
  • exports.findAll = function(){ Game.find().lean().exec(function (err, games) { if (err) return console.error(err); console.log(games); return (JSON.stringify(games)); }), In my shell: [ { _id: 5977ff7252dd664d88e45163, title: 'opa', imagePath: '../public/images/opa.jpg', __v: 0 }, { _id: 5977ff9052dd664d88e45164, title: 'fallout 4', imagePath: '../public/images/fallout 4.jpg', __v: 0 } ] – Leonardo Bonetti Jul 26 '17 at 04:11

0 Answers0