gpt4 book ai didi

mongodb - Bootstrap 模式对话框内的 meteor 模板渲染

转载 作者:可可西里 更新时间:2023-11-01 10:05:12 26 4
gpt4 key购买 nike

当我尝试显示从 Bootstrap 模态对话框中的集合获取的值时,我遇到了这个问题。

这是客户端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 之后再渲染模板?

最佳答案

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

之前检查是否有记录
Template.Modal_edit_client.edit_client_name = function() {
var c = Clients.findOne({_id: Session.get("current_editing")});
if(c) return c.name;
};

关于mongodb - Bootstrap 模式对话框内的 meteor 模板渲染,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15397913/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com