【问题标题】:Cannot read property of undefined in Jade无法读取 Jade 中未定义的属性
【发布时间】:2015-02-01 06:15:51
【问题描述】:

我写了一个小玉脚本就是

ul.sidebar-nav
  each(record in #{records})
    li
      a(href='#') #{record.sender} 

我正在将一个记录数组对象传递给翡翠模板,但对于record 未定义。谁能解释一下为什么我会收到这个错误?

这是完整的错误:

/home/ritesh/Sample/Sample1/Sample/views/index.jade:9 7| each(record in #{records}) 8| li > 9| a(href='#') #{record.sender} 10| Cannot read property 'sender' of undefined

TypeError: /home/ritesh/Sample/Sample1/Sample/views/index.jade:9
    7|         each(record in #{records})
    8|           li
  > 9|             a(href='#') #{record.sender}         
    10| 

Cannot read property 'sender' of undefined
    at eval (eval at <anonymous> (/home/ritesh/Sample/Sample1/Sample/node_modules/jade/lib/index.js:216:8), <anonymous>:83:49)
    at eval (eval at <anonymous> (/home/ritesh/Sample/Sample1/Sample/node_modules/jade/lib/index.js:216:8), <anonymous>:110:22)
    at res (/home/ritesh/Sample/Sample1/Sample/node_modules/jade/lib/index.js:217:38)
    at Object.exports.renderFile (/home/ritesh/Sample/Sample1/Sample/node_modules/jade/lib/index.js:360:38)
    at View.exports.renderFile [as engine] (/home/ritesh/Sample/Sample1/Sample/node_modules/jade/lib/index.js:350:21)
    at View.render (/home/ritesh/Sample/Sample1/Sample/node_modules/express/lib/view.js:93:8)
    at EventEmitter.app.render (/home/ritesh/Sample/Sample1/Sample/node_modules/express/lib/application.js:530:10)
    at ServerResponse.res.render (/home/ritesh/Sample/Sample1/Sample/node_modules/express/lib/response.js:933:7)
    at /home/ritesh/Sample/Sample1/Sample/routes/index.js:13:7
    at /home/ritesh/Sample/Sample1/Sample/node_modules/mongojs/node_modules/mongodb/lib/mongodb/cursor.js:172:16   

【问题讨论】:

  • 试试each record in records,注意记录应该是一个数组。

标签: javascript node.js pug


【解决方案1】:
  each(record in #{records})

你不想在这里解析records;您只想将其用作数组。只需使用

  each(record in records)

它应该可以工作。

【讨论】:

    【解决方案2】:

    只需使用:

    each record in records
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-06
      • 2020-10-30
      相关资源
      最近更新 更多