使用swig / twig创建forEach循环

时间:2017-10-16 22:31:06

标签: javascript node.js express swig-template

我正在尝试创建一个循环,将值输出到表中。到目前为止我所拥有的并不会在渲染页面上的任何地方加载。什么都没有显示出来。我不太确定如何使用swig / twig进行循环,所以不知道在这篇文章中包含什么。请查看代码,如果您需要其他内容,我会根据要求添加。我尝试循环:

{%  for aps in aps  %}
  <tr>
   <td>{{aps.name}}</td>
   <td>{{aps.manufacturer}}</td>
   <td>{{aps.model}}</td>
   <td>{{aps.name}}</td>
   <td>{{aps.notes}}</td>
  </tr>
{% endfor %}

型号:

var accessPointsSchema = new Schema({
  name: {String},
  manufacturer: {type: String},
  model: {type: String},
  IPAdress: {type: String},
  MACAdress: {type: String},
  range: {type: Number()},
  bands: {type: String},
  channel: {type: Number},
  dateBought: {type: Date},
  PoE: {type: Boolean},
  assetNumber: {type: Number},
  warrantyExpiration: {type: Date},
  location: {type: String},
  notes: {type: String},
  author: {
      id: {
         type: mongoose.Schema.Types.ObjectId,
         ref: "User"
      },
      email: String
   }
});

路线:

// show model
  app.get('/dashboard/it/model',
    setRender('dashboard/it/model'),
    setRedirect({auth: '/login'}),
    isAuthenticated,
    dashboard.getDefault,
    (req, res) => {
      console.log("Reached end/start thing");
      AP.find({}, function(err, allAP){
        console.log(allAP);
       if(err){
         console.log("Reached if");
           console.log(err);
       } else {
         console.log("Reached else");
          setRender('dashboard/it/model',{aps:allAP});
       }
    });
    });

1 个答案:

答案 0 :(得分:0)

我将根据您在https://github.com/joshk132/asset-management

发布的代码尝试回答

首先,setRender似乎没有采取第二个论点。所以请尝试使用类似的东西:

app.get('/dashboard/it/model',
    setRender('dashboard/it/model'),
    setRedirect({auth: '/login'}),
    isAuthenticated,
    (req, res, next) => { // Moved this before getDefault
        console.log("Reached end/start thing");
        AP.find({}, function(err, allAP) {
            console.log(allAP);
            if(err){
                console.log("Reached if");
                console.log(err);
                next(err);
            } else {
                console.log("Reached else");
                res.locals.aps = allAP; // Set the data in locals
                next();
            }
        });
    },
    dashboard.getDefault
);

重要的是,在调用dashboard.getDefault getDefault调用res.render之前,我已经移动了数据库内容,所有数据都需要在发生之前加载。

您还需要在next()结束时处理getDefault来电,因为没有其他事可做。

模板将是这样的:

{%  for ap in aps  %}
  <tr>
    <td>{{ap.name}}</td>

等等。