2014-06-15 64 views
0

我正在爲項目使用sails JS和Jade模板引擎。使用變量爲圖像src屬性渲染視圖時出現錯誤

我玉的觀點是:

h2.center= publication.title 
img(src = publication.path) //- i've this error with this line 

但在終端顯示的錯誤是:

Cannot read property 'title' of undefined 

時,請檢查生成的I HTML一切都很好:

<h2 class="center">My title</h2> 
<img src="img/publication2.png" /> 

謝謝您幫幫我 !

編輯1:

我有這個代碼在我的控制器:

find : function(req, res) { 
    Publication.findOne(req.param('id')).done(function(err, obj) { 
     if (err) { 
      console.log ("err", err) 
     } else { 
      return res.view({ 
       publication : obj 
      }) 
     } 
    }) 
}, 

EDIT 2,全面的錯誤跟蹤:

Error rendering view at :: 
error: Using layout located at :: 
error: Server Error (500) 
error: TypeError: /home/toni/nodeJS/h2d/views/publication/find.jade:5 
Cannot read property 'title' of undefined 

只是爲了測試我更換

img(src = publication.path) 

h2= plubication.path 

,我已經沒有錯誤

回答

0

你怎麼跑jade?你似乎運行它沒有--client選項,這意味着它試圖將模板直接編譯爲HTML,而不是JS函數(所以它試圖解析變量並直接替換它們)。

使用正確的選項(--client)或.compileClient(如果您使用的是API),它應該工作(TM)。

+0

請編輯您的問題 – Ven

+0

心中已經編輯我的第一個問題 – amiceli

+0

你完全確定'obj'不爲空? – Ven