2016-02-26 59 views
0

我試圖增加分數測驗中的應用程序用戶,但我得到一個錯誤「遺漏的類型錯誤:無法讀取屬性‘簡表’未定義「代碼」var correctAns = account.profile.score;「。我使用的是AccountsUser包計分測驗中的應用(如何增加測驗中的應用分)

Template.quiz.events({ 
     'click .next': function (evt,template) { 
     // countdown.start(function() { 
       evt.preventDefault(); 
       Session.set("questionNumber", Session.get("questionNumber") + 1); 
       var element = template.find('input:radio[name=k]:checked'); 
       // var inputValue = template.find('#myId').value; 

       console.log($(element).val()); 
       var Id = this._id; 
       var account = Meteor.users.findOne({_id: Id}); 
       var correctAns = account.profile.score; 
        console.log(correctAns); 
       if (element == correctAns) { 
        Meteor.users.update({_id:Id}, {$inc:{'profile.score': 2}}, function(err){ 
         if(err){ 
          console.log(err); 
         } else { 
          console.log('updated') 
         } 
        }) 
       } 

我的用戶數據庫如下:

{ 
"_id": "MNg6dJsWKyEETy5ej", 
"profile": { 
"firstname": "le", 
"lastname": "me", 
"phonenumber": "02929", 
"user": "Staff", 
"AccountStatus": "Activated", 
"score": 0 
}, 
"username": "11" 
} 
+2

錯誤意味着用戶無法找到。這可能是由於以下幾個原因:(1)'this._id'不是你認爲的那樣,(2)id是有效的,但是用戶不是當前用戶,並且它沒有'已發佈給客戶,等 –

回答

0

您需要Meteor.userId()而不是測驗ID來update用戶。

Id現在是用戶的ID和question_id是不言自明的。

Template.quiz.events({ 
    'click .next': function (evt,template) { 
    // countdown.start(function() { 
      evt.preventDefault(); 
      Session.set("questionNumber", Session.get("questionNumber") + 1); 
      var element = template.find('input:radio[name=k]:checked'); 
      // var inputValue = template.find('#myId').value; 

      console.log($(element).val()); 
      var Id = Meteor.userId(); 
      var trys = $(element).val(); 
       // console.log(Id); 
      var question_ID = this._id; 
      var account = Questions.findOne({_id: question_ID}); 
      var correctAns = account.correctAns; 
       console.log(correctAns); 
       console.log(trys); 
      if (trys == correctAns) { 
       Meteor.users.update({_id:Id}, {$inc: {'profile.score': 2}}, function(err){ 
        if(err){ 
         console.log(err); 
        } else { 
         console.log('updated') 
        } 
       }) 
      } 
+0

只有代碼的答案是不鼓勵... –