2013-03-13 108 views
2

我有這個問題,當我試圖顯示一個值從引導模式對話框中的集合獲取。流星模板呈現在引導模式對話框內

這是客戶端的JavaScript代碼:

Template.Modal_edit_client.edit_client_name = function() { 
    var c = Clients.findOne({_id: Session.get("current_editing")}); 
    return c.name; 
}; 

,這是模板:

<template name="Modal_edit_client"> 
    <div id="Modal_edit_client" class="modal hide fade"> 
     <div class="modal-header"> 
      <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button> 
      <h3>Editar cliente</h3> 
     </div> 
     <div class="modal-body"> 
      <form id="edit_client" action=""> 
       <fieldset> 
        <input id="edit_client_name" placeholder="Nombre y apellido" type="text" value="{{edit_client_name}}"> 
        {{edit_client_name}} 
       </fieldset> 
      </form> 
     </div> 
     <div class="modal-footer"> 
      <a id="edit_client_cancel" href="#" class="btn">Cancelar</a> 
      <a id="edit_client_save" href="#" class="btn btn-primary">Guardar</a> 
     </div> 
    </div> 
</template> 

頁沒有呈現,這就是顯示conosole:

Uncaught TypeError: Cannot read property 'name' of undefined 
Exception from Meteor.flush: TypeError: Cannot call method 'firstNode' of undefined 
    at Object.Spark.renderToRange (http://localhost:3000/packages/spark/spark.js?ba288278f8e36e3529187cea4590001f50ef0f95:545:25) 
    at http://localhost:3000/packages/spark/spark.js?ba288278f8e36e3529187cea4590001f50ef0f95:860:13 
    at http://localhost:3000/packages/deps/deps-utils.js?0c00e493224f891c3d6c82a23693ba55e0f47611:78:31 
    at _.extend.run (http://localhost:3000/packages/deps/deps.js?d804638a4633c2f6488827923ba5fbd00f07d518:19:20) 
    at rerun (http://localhost:3000/packages/deps/deps-utils.js?0c00e493224f891c3d6c82a23693ba55e0f47611:78:11) 
    at http://localhost:3000/packages/deps/deps.js?d804638a4633c2f6488827923ba5fbd00f07d518:71:15 
    at Array.forEach (native) 
    at Function._.each._.forEach (http://localhost:3000/packages/underscore/underscore.js?017a0dea6ebb07eec57a1541a0fd524665e769bd:79:11) 
    at http://localhost:3000/packages/deps/deps.js?d804638a4633c2f6488827923ba5fbd00f07d518:69:13 
    at Array.forEach (native) logging.js:30 
Exception from Meteor.flush: TypeError: Cannot call method 'firstNode' of undefined 
    at Object.Spark.renderToRange (http://localhost:3000/packages/spark/spark.js?ba288278f8e36e3529187cea4590001f50ef0f95:545:25) 
    at http://localhost:3000/packages/spark/spark.js?ba288278f8e36e3529187cea4590001f50ef0f95:860:13 
    at http://localhost:3000/packages/deps/deps-utils.js?0c00e493224f891c3d6c82a23693ba55e0f47611:78:31 
    at _.extend.run (http://localhost:3000/packages/deps/deps.js?d804638a4633c2f6488827923ba5fbd00f07d518:19:20) 
    at rerun (http://localhost:3000/packages/deps/deps-utils.js?0c00e493224f891c3d6c82a23693ba55e0f47611:78:11) 
    at http://localhost:3000/packages/deps/deps.js?d804638a4633c2f6488827923ba5fbd00f07d518:71:15 
    at Array.forEach (native) 
    at Function._.each._.forEach (http://localhost:3000/packages/underscore/underscore.js?017a0dea6ebb07eec57a1541a0fd524665e769bd:79:11) 
    at http://localhost:3000/packages/deps/deps.js?d804638a4633c2f6488827923ba5fbd00f07d518:69:13 
    at Array.forEach (native) logging.js:30 
Exception from Meteor.flush: TypeError: Cannot call method 'firstNode' of undefined 
    at Object.Spark.renderToRange (http://localhost:3000/packages/spark/spark.js?ba288278f8e36e3529187cea4590001f50ef0f95:545:25) 
    at http://localhost:3000/packages/spark/spark.js?ba288278f8e36e3529187cea4590001f50ef0f95:860:13 
    at http://localhost:3000/packages/deps/deps-utils.js?0c00e493224f891c3d6c82a23693ba55e0f47611:78:31 
    at _.extend.run (http://localhost:3000/packages/deps/deps.js?d804638a4633c2f6488827923ba5fbd00f07d518:19:20) 
    at rerun (http://localhost:3000/packages/deps/deps-utils.js?0c00e493224f891c3d6c82a23693ba55e0f47611:78:11) 
    at http://localhost:3000/packages/deps/deps.js?d804638a4633c2f6488827923ba5fbd00f07d518:71:15 
    at Array.forEach (native) 
    at Function._.each._.forEach (http://localhost:3000/packages/underscore/underscore.js?017a0dea6ebb07eec57a1541a0fd524665e769bd:79:11) 
    at http://localhost:3000/packages/deps/deps.js?d804638a4633c2f6488827923ba5fbd00f07d518:69:13 
    at Array.forEach (native) 

我認爲問題顯示在第一行,Uncaught TypeError: Cannot read property 'name' of undefined。有沒有辦法等到c.name !== undefined和之後呈現模板?

+0

對於大型的代碼之前的記錄,[要點](http://gist.github.com)是一個很好的選項 – Prashant 2013-03-14 01:27:12

回答

3

當流星在瀏覽器中第一次加載時,它沒有來自集合中服務器的任何數據,但模板仍然會呈現。就像你的Session沒有設置一樣。所以,你需要處理的情況下,而它的加載(只是檢查是否有試圖讀取name

Template.Modal_edit_client.edit_client_name = function() { 
    var c = Clients.findOne({_id: Session.get("current_editing")}); 
    if(c) return c.name; 
}; 
+0

謝謝!這真的很有幫助,但我仍然無法看到模板中呈現的值。 – rec 2013-03-14 04:16:33

+0

您是否爲「current_editing」設置了會話哈希值,並且它與您的集合中的某個ID相對應?如果運行「客戶端」。 findOne({_ id:Session.get(「current_editing」)});'在你的webkit控制檯中它是否給你你的文檔? – Akshat 2013-03-14 08:47:07

+0

是的!正是我想要的,給了我文檔的ID – rec 2013-03-17 00:09:51