我试图在我的模板中使用apsingle,但它不起作用。当我console.log(apsingle);时,我得到了正确的数据,但是在模板中,它根本不起作用。它回来了
部分路线:
(req, res, next) => {
AP.findById(req.params.id).exec(function(err, foundAP){
if(err){
console.log(err);
} else {
res.locals.apsingle = foundAP;
}
});
next();
}循环和if语句在模板中:
{% if apsingle %}
{% for ap in apsingle %}
<tr>
<td>{{ap.type}}</td>
<td>{{ap.model}}</td>
<td>{{ap.notes}}</td>
</tr>
{% endfor %}
{% endif %}如果我做了一个测试:
{% if apsingle == null %}
<h1>I'm NULL</h1>
{% endif %}然后它输出它,因此apsingle以null的形式传递到模板。
安迪要求的输出:
{ _id: objectID,
type: 'ap',',
model: ';lkj;l',
notes: '',
__v: 0,
author: id: someID
}向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)发布于 2017-10-21 00:38:18
小心范围,把它放在一个括号内,这并不意味着它会引起很多问题。
开始:
(req, res, next) => {
AP.findById(req.params.id).exec(function(err, foundAP){
if(err){
console.log(err);
} else {
res.locals.apsingle = foundAP;
}
});
next();
}已解决:
(req, res, next) => {
AP.findById(req.params.id).exec(function(err, foundAP){
if(err){
console.log(err);
} else {
res.locals.apsingle = foundAP;
}
});
}
next();发布于 2017-10-20 22:02:26
尝试呈现一个页面以排除故障。也许您需要一种不同的方法,创建另一个文件并尝试填充它。然后您可以使用res.locals再试一次
(req, res, next) => {
AP.findById(req.params.id, (err, foundAP) => {
if(err){
console.log(err);
} else {
console.log(foundAP);
res.render('yourview', {apsingle : foundAP)
}
});
next();
}在你看来试试这个。你不是在拉一个对象数组,而只是一个。你不需要绕着它转。
{% if apsingle %}
<tr>
<td>{{apsingle.type}}</td>
<td>{{apsingle.model}}</td>
<td>{{apsingle.notes}}</td>
</tr>
{% endif %}https://stackoverflow.com/questions/46857554
复制相似问题