【问题标题】:Meteor template render inside a bootstrap modal dialog流星模板在引导模式对话框中渲染
【发布时间】:2013-03-02 03:09:24
【问题描述】:

当我试图显示从引导模式对话框中的集合中获取的值时,我遇到了这个问题。

这是客户端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>

页面未呈现,这就是控制台显示的内容:

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之后再渲染模板?

【问题讨论】:

  • 对于大代码,gist 是一个不错的选择

标签: mongodb twitter-bootstrap meteor


【解决方案1】:

当流星第一次在浏览器上加载时,它的集合中没有来自服务器的任何数据,但模板仍会呈现。与未设置会话一样。所以你需要在加载时处理这个案例(在尝试读取name之前检查是否有记录@

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

【讨论】:

  • 谢谢!这真的很有帮助,但我仍然看不到模板中呈现的值。
  • 您是否为“current_editing”设置了会话哈希,它是否与您收藏中的 id 相对应?如果您在 webkit 控制台中运行Clients.findOne({_id: Session.get("current_editing")});,它会为您提供文档吗?
  • 是的!正是我想要的,给我文档的 ID。
  • 这确实有效,我重新启动服务器,现在正在工作,谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-06-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-12-07
  • 1970-01-01
  • 2013-11-16
相关资源
最近更新 更多