Reputation: 1
UPDATED
I want to print all the items from the database reizen (in Mongoose) that I created. It never prints the items it always prints immediatly prints 'Geen reizen'. I've been looking for quite some time and I don't know how to fix it. My code is:
var router = express.Router();
const mongoose = require('mongoose');
const { body, validationResult } = require('express-validator/check');
/* GET reizen*/
router.get('/', function(req, res, next) {
if (req.cookies.username != '') {
res.render('reizen', { sisu : req.cookies.username });
}
else {
res.render('reizen', { sisu : 'SIGN IN/SIGN UP'});
}
});
router.get('/getreizen', function(req, res, next) {
reizen.find({}, function(err, reizen) {
res.render('reizen', {reizen: reizen});
});
});
I'm still working on the first part, about the cookies, so you can ignore that part.
My code in pug is:
extends layout
block content
#reizen.w3-content.w3-container.w3-padding-64
h3.w3-center REIZEN
a.w3-bar-item.border-button(href='/reistoevoegen')
| VOEG REIS TOE
p
|
|
p
each val in reizen
a Titel: #{val.titel}
else
li Geen reizen.
I'm using each val in reizen ? reizen: []
because otherwise I get the error 'can't get property length from undefined'.
Can somebody tell me what's wrong?
Upvotes: 0
Views: 139
Reputation: 1831
Your method signature is wrong, you are using:
reizen.find({}, function(reizen) {
res.render('reizen', {reizen: reizen});
});
But the callback has the signature function(err, reizen)
.
So you are trying to print the error (err
) in your template which is undefined.
Update:
Next Test is to exclude the db from the equation. Update code and run again.
const data = [
{ titel: "A" },
{ titel: "B" },
]
res.render('reizen', {reizen: data});
Upvotes: 1