Res.locals.apsinlge not working

240 Views Asked by At

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)
2

There are 2 best solutions below

0
Kirbytech On BEST ANSWER

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();
18
Andy On

Try rendering a page to troubleshoot. Maybe you need a different approach, create another file and try to populate it. Then you can try again with 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();
}

In your view try this. You are not pulling an array of object, but just one. You don't need to loop thru it.

{% if apsingle %}
 <tr>
  <td>{{apsingle.type}}</td>
  <td>{{apsingle.model}}</td>
  <td>{{apsingle.notes}}</td>
 </tr>
{% endif %}