简体   繁体   English

Pug-Jade模板访问模块数据

[英]Pug-Jade template accessing modules data

Edit : I'm trying to create a pug-jade template in which a table is created, the first row of the table is static (ie some headers ) for each subsequent row i want to add an employee. 编辑:我正在尝试创建一个在其中创建表格的哈巴狗模板,该表格的第一行是静态的(即某些标头),用于我要添加员工的每一行。 I have 3 files express.js (handels request to my app) employee.js, and index.pug. 我有3个文件express.js(处理请求到我的应用程序)employee.js和index.pug。
Here is what i export in employee.js 这是我在employee.js中导出的​​内容

function Employee (id,name,surname,lvl,salary){
this.id = id;
this.name = name;}
exports.employees = {
employeeList: [],
//the empList is a list of employee defined above, class contains some other methods   
}  

In the express.js i call : 在express.js中,我致电:

var employees = require('./employees')

inside the request handler i call : 在我调用的请求处理程序内:

res.render('index',{employees: employees})

and finally in index.pug i try to : 最后在index.pug中,我尝试:

tbody
    for val in employees.employeeList
        td=val.id
        td=val.name   

And this is the error : 这是错误:

TypeError: /Users/filippobosco/Desktop/GitHubRepos/IngSoftware2/Employees/ExpressTutorial/views/index.pug:39   

  37| th Salary
  38| tbody
> 39| for val in employees.employeeList
  40| td=val.id
  41| td=val.name
  42| 

  Cannot read property 'length' of undefined

I hope it is more clear now. 我希望现在更加清楚。

Your employees variable is undefined . 您的employees变量undefined

This means that there is a problem with your require('employees') import. 这意味着您的require('employees')导入存在问题。 Without being able to see the employees.js files, I'd have to guess that you're exports are not set up correctly. 在无法看到employees.js文件的情况下,我不得不猜测您exports文件设置不正确。

Once you've dealt with the suggestions above... 处理完上述建议后...

I came here from having a similar issue and what worked for me would have been something like this: 我是从一个类似的问题来到这里的,对我有用的是这样的事情:

tbody
for val in employees.employeeList
    td #{val.id}
    td #{val.name}  

NOTE: I am using pug 3.10.6 注意:我正在使用哈巴狗3.10.6

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM