在模板的事件处理程序中获取数据

时间:2016-05-10 00:56:28

标签: meteor

我想测试一些关于我存储在一个名为exams的集合中存储的数据的东西。这是我的代码

Template.Examinations.events({
  'click .reactive-table tr': function() {
    Session.set('selectedPersonId', this._id); 
    var cursor = Exam.findOne(Session.get("selectedPersonId"));
    if (!cursor.count()) return;
      cursor.forEach(function(doc){
      console.log(doc._id);
    });    
  },
});

每次单击一行运行代码时,都会出现错误

  

未捕获的TypeError:cursor.count不是函数

为什么我收到此错误?

更新

{
    "_id" : "RLvWTcsrbRXJeTqdB",
    "examschoolid" : "5FF2JRddZdtTHuwkx",
    "examsubjects" : [
        {
            "subject" : "Z4eLrwGwqG4pw4HKX"
        },
        {
            "subject" : "fFcWby8ArpboizcT9"
        }
    ],
    "examay" : "NrsP4srFGfkc5cJkz",
    "examterm" : "5A5dNTgAkdRr5j53j",
    "examclass" : "gYF2wE4wBCRy9a3ZC",
    "examname" : "First",
    "examdate" : ISODate("2016-05-07T22:41:00Z"),
    "examresultsstatus" : "notreleased"
}

1 个答案:

答案 0 :(得分:1)

您正在使用Exam.findOne,它将返回一个对象而不是数组,这会导致错误cursor.count is not a function。您应该使用Exam.find({}).fetch(),然后您可以从结果中获取计数。

Template.Examinations.events({
  'click .reactive-table tr': function() {
    Session.set('selectedPersonId', this._id); 
    var examsArray = Exam.find({ personId: this._id}).fetch();
    examsArray.forEach(function(doc){
      console.log(doc._id);
    });
  },
});
相关问题