【问题标题】:Exception from Deps recompute function: TypeError: Cannot read property 'option1' of undefined来自 Deps 重新计算函数的异常:TypeError:无法读取未定义的属性“option1”
【发布时间】:2014-07-25 12:30:21
【问题描述】:

我有一个渲染良好的页面,每当我刷新页面时,它都会显示异常 如下所示

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>

帮我解决这个问题。 如果您需要任何其他信息,我会提供。

【问题讨论】:

    标签: meteor meteorite


    【解决方案1】:

    问题在于Polls_Coll.findOne()。当您加载页面时,尚未从数据库中获取数据,因此所有查找查询都是空的 - 因此,Polls_Coll.findOne(...) 返回未定义。只是稍后才重新运行帮助程序,findOne 返回实际数据。

    最简单的解决方法是检查数据是否到位:

    Template.displayPoll.productTitle = function() {
      var poll = Polls_Coll.findOne(this._id);
      if(!poll) return '';
      return poll.option1[0].pd;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-01
      • 2022-01-21
      • 1970-01-01
      • 1970-01-01
      • 2019-12-20
      • 2015-09-04
      相关资源
      最近更新 更多