我有一个呈现正常的页面,每当我刷新页面时它都显示异常 如下所示
Exception from Deps recompute function: TypeError: Cannot read property 'option1' of undefined
at Object.Template.display_poll.helpers.product_title1 (http://localhost:3000/client/xxx.js?9b4f281e748352d5500dbda34a33f155fc8cc293:838:49)
at apply (http://localhost:3000/packages/handlebars.js?c2b75d49875b4cfcc7544447aad117fd81fccf3b:276:24)
at invoke (http://localhost:3000/packages/handlebars.js?c2b75d49875b4cfcc7544447aad117fd81fccf3b:301:12)
at http://localhost:3000/packages/handlebars.js?c2b75d49875b4cfcc7544447aad117fd81fccf3b:365:30
at Object.Spark.labelBranch (http://localhost:3000/packages/spark.js?742c715b73fac26c16ad433118b87045bc5658ff:1170:14)
at branch (http://localhost:3000/packages/handlebars.js?c2b75d49875b4cfcc7544447aad117fd81fccf3b:355:20)
at http://localhost:3000/packages/handlebars.js?c2b75d49875b4cfcc7544447aad117fd81fccf3b:364:18
at Array.forEach (native)
at Function._.each._.forEach (http://localhost:3000/packages/underscore.js?0a80a8623e1b40b5df5a05582f288ddd586eaa18:156:11)
at template (http://localhost:3000/packages/handlebars.js?c2b75d49875b4cfcc7544447aad117fd81fccf3b:358:7)
异常导致行是
product_title1:function() {
var title1=Polls_Coll.findOne({_id:this._id}).option1[0].pd;
return title1;
}
这是我的模板
<template name="display_poll">
<!-- displaying poll question -->
{{uniqueid}}
<p class="label-design table-pop-more" name="{{unique_id}}">
<img src="/images/polls.jpg">{{question}}</p>
<div class="table-pop-more">
<div class="poll-options" id="menuitems">
<ul>
<!-- product thumbnails -->
<li><input type="text" class="pad-top" id="{{id_op1}}"
value="{{product_title1}}" readonly/><br></li>
<li><input type="text" class="pad-top" id="{{id_op2}}"
value="{{product_title2}}" readonly/><br></li>
<li><input type="text" class="pad-top" id="{{id_op3}}"
value="{{product_title3}}" readonly/><br></li>
<li><input type="text" class="pad-top" id="{{id_op4}}"
value="{{product_title4}}" readonly/><br></li>
</ul>
</div>
</div>
</template>
帮助我。 如果您需要我将提供的任何其他信息。
答案 0 :(得分:4)
问题在于Polls_Coll.findOne()
。加载页面时,尚未提取数据库中的数据,因此所有查找查询都为空 - 因此Polls_Coll.findOne(...)
返回undefined。只有在稍后重新运行帮助程序并且findOne
才会返回实际数据。
最简单的解决方案是检查数据是否到位:
Template.displayPoll.productTitle = function() {
var poll = Polls_Coll.findOne(this._id);
if(!poll) return '';
return poll.option1[0].pd;
}