i’m trying to add scores for a user in a quiz app, but i get an error “Uncaught TypeError: Cannot read property ‘profile’ of undefined” on the code “var correctAns = account.profile.score;”. i’m using the AccountsUser Package
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')
}
})
}
my users database is as follows:
{
"_id": "MNg6dJsWKyEETy5ej",
"profile": {
"firstname": "le",
"lastname": "me",
"phonenumber": "02929",
"user": "Staff",
"AccountStatus": "Activated",
"score": 0
},
"username": "11"
}