2012-10-02 88 views
2

我有這個開始導致下面的TypeError的jade模板。編譯Jade模板時出現TypeError

TypeError: /home/jared/code/fiestah-admin/views/items.jade:11 
    9|   a(href=urlFor('item details', { 
    10|   params: {  
\> 11|    //id: item._id 
    12|   } 
    13|   })) #{item.name} 
    14|   .planner By 

Cannot read property '_id' of null 
    at list_mixin (eval at <anonymous> (/home/jared/code/fiestah-admin/node_modules/jade/lib/jade.js:176:8)) 
    at list_mixin (eval at <anonymous> (/home/jared/code/fiestah-admin/node_modules/jade/lib/jade.js:176:8)) 
    at eval (eval at <anonymous> (/home/jared/code/fiestah-admin/node_modules/jade/lib/jade.js:176:8)) 
    at Object.exports.compile (/home/jared/code/fiestah-admin/node_modules/jade/lib/jade.js:181:12) 
    at ServerResponse.res._render (/home/jared/code/fiestah-admin/node_modules/express/lib/view.js:425:21) 
    at ServerResponse.res.render (/home/jared/code/fiestah-admin/node_modules/express/lib/view.js:318:17) 
    at render (/home/jared/code/fiestah-admin/controllers/items-controller.js:23:7) 
    at module.exports (/home/jared/code/fiestah-admin/controllers/items-controller.js:41:5) 
    at Request._callback (/home/jared/code/fiestah-admin/node_modules/lib/libraries/api.js:95:5) 
    at Request.init.self.callback (/home/jared/code/fiestah-admin/node_modules/request/main.js:120:22) 

說有產權_id,但我不認爲它實際上是屬於item._id。如果我將該行註釋掉或在id之前添加另一個參數,則行號會更改,但異常保持不變。我甚至在模板中刪除了兩個對_id的引用,並且異常是相同的。

這裏是模板:

.page-header 
    h1 Items 

mixin list(items) 
    hr 
    ul.index 
    - each item in items 
     li 
     a(href=urlFor('item details', { 
      params: { 
      id: item._id 
      } 
     })) #{item.name} 
     .planner By 
      | 
      a(href=urlFor('user details', { 
      params: { 
       id: item.planner._id 
      } 
      })) #{item.planner.name.full} 

.row 
    .span4 
    h1 Open Items (#{openItems.length}) 
    mixin list(openItems) 
.row 
    .span4 
    h1 Open Items - No Offers (#{emptyItems.length}) 
    mixin list(emptyItems) 
.row 
    .span4 
    h1 Past Items (#{pastItems.length}) 
    mixin list(pastItems) 

我檢查什麼是三個「項目」陣列傳遞,而且全是空的,或者包含具有_id領域的有效對象。數組中沒有空成員。

回答

2

在內嵌代碼,這樣做:

.row 
    .span4 
    h1 Open Items (#{openItems.length}) 
     +dolist(openItems) 

,而不是這樣的:

.row 
    .span4 
    h1 Open Items (#{openItems.length}) 
     mixin dolist(openItems)