首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Res.locals.apsinlge不工作

Res.locals.apsinlge不工作
EN

Stack Overflow用户
提问于 2017-10-20 21:25:22
回答 2查看 229关注 0票数 0

我试图在我的模板中使用apsingle,但它不起作用。当我console.log(apsingle);时,我得到了正确的数据,但是在模板中,它根本不起作用。它回来了

部分路线:

代码语言:javascript
复制
(req, res, next) => {
      AP.findById(req.params.id).exec(function(err, foundAP){
        if(err){
            console.log(err);
        } else {
            res.locals.apsingle =  foundAP;
        }
    });
    next();
    }

循环和if语句在模板中:

代码语言:javascript
复制
{% if apsingle %}
   {%  for ap in apsingle  %}
     <tr>
      <td>{{ap.type}}</td>
      <td>{{ap.model}}</td>
      <td>{{ap.notes}}</td>
     </tr>
  {% endfor %}
{% endif %}

如果我做了一个测试:

代码语言:javascript
复制
{% if apsingle == null %}
<h1>I'm NULL</h1>
{% endif %}

然后它输出它,因此apsinglenull的形式传递到模板。

安迪要求的输出:

代码语言:javascript
复制
{ _id: objectID,
  type: 'ap',',
  model: ';lkj;l',

  notes: '',
  __v: 0,
  author:  id: someID
}

向Andy提到的错误:

代码语言:javascript
复制
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)
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-10-21 00:38:18

小心范围,把它放在一个括号内,这并不意味着它会引起很多问题。

开始:

代码语言:javascript
复制
(req, res, next) => {
      AP.findById(req.params.id).exec(function(err, foundAP){
        if(err){
            console.log(err);
        } else {
            res.locals.apsingle =  foundAP;
        }
    });
    next();
    }

已解决:

代码语言:javascript
复制
(req, res, next) => {
  AP.findById(req.params.id).exec(function(err, foundAP){
    if(err){
        console.log(err);
    } else {
        res.locals.apsingle =  foundAP;
    }
});
}
next();
票数 0
EN

Stack Overflow用户

发布于 2017-10-20 22:02:26

尝试呈现一个页面以排除故障。也许您需要一种不同的方法,创建另一个文件并尝试填充它。然后您可以使用res.locals再试一次

代码语言:javascript
复制
(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();
}

在你看来试试这个。你不是在拉一个对象数组,而只是一个。你不需要绕着它转。

代码语言:javascript
复制
{% if apsingle %}
 <tr>
  <td>{{apsingle.type}}</td>
  <td>{{apsingle.model}}</td>
  <td>{{apsingle.notes}}</td>
 </tr>
{% endif %}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46857554

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档