I am trying to use apsingle
in my template but it is not working. I get the correct data when I console.log(apsingle);
but inside the template it just isn't working at all. It return
Partial route:
(req, res, next) => {
AP.findById(req.params.id).exec(function(err, foundAP){
if(err){
console.log(err);
} else {
res.locals.apsingle = foundAP;
}
});
next();
}
Loop and if statement inside template:
{% if apsingle %}
{% for ap in apsingle %}
<tr>
<td>{{ap.type}}</td>
<td>{{ap.model}}</td>
<td>{{ap.notes}}</td>
</tr>
{% endfor %}
{% endif %}
If I do a test of:
{% if apsingle == null %}
<h1>I'm NULL</h1>
{% endif %}
Then it outputs it, so the apsingle
is coming through to the template as null
.
Output asked for by Andy:
{ _id: objectID,
type: 'ap',',
model: ';lkj;l',
notes: '',
__v: 0,
author: id: someID
}
Error mentioned to Andy:
TypeError: Cannot read property 'name' of undefined
at Object.eval [as tpl] (eval at <anonymous> (/home/ubuntu/workspace/asset-management/node_modules/swig/lib/swig.js:498:13), <anonymous>:10:1706)
at compiled (/home/ubuntu/workspace/asset-management/node_modules/swig/lib/swig.js:619:18)
at Object.eval [as tpl] (eval at <anonymous> (/home/ubuntu/workspace/asset-management/node_modules/swig/lib/swig.js:498:13), <anonymous>:7:154)
at compiled (/home/ubuntu/workspace/asset-management/node_modules/swig/lib/swig.js:619:18)
at /home/ubuntu/workspace/asset-management/node_modules/swig/lib/swig.js:559:20
at /home/ubuntu/workspace/asset-management/node_modules/swig/lib/swig.js:690:9
at tryToString (fs.js:456:3)
at FSReqWrap.readFileAfterClose [as oncomplete] (fs.js:443:12)
Be careful of scope, having it inside one bracket that you don't mean it to be causes a lot of issues.
Started:
(req, res, next) => {
AP.findById(req.params.id).exec(function(err, foundAP){
if(err){
console.log(err);
} else {
res.locals.apsingle = foundAP;
}
});
next();
}
Solved:
(req, res, next) => {
AP.findById(req.params.id).exec(function(err, foundAP){
if(err){
console.log(err);
} else {
res.locals.apsingle = foundAP;
}
});
}
next();