Javascript 测验应用程序中的分数(如何在测验应用程序中增加分数)
我试图在测验应用程序中为用户添加分数,但在代码“var correctAns=account.profile.score;”上出现错误“Uncaught TypeError:无法读取未定义的属性'profile'。我正在使用AccountsUser软件包Javascript 测验应用程序中的分数(如何在测验应用程序中增加分数),javascript,mongodb,meteor,meteor-accounts,Javascript,Mongodb,Meteor,Meteor Accounts,我试图在测验应用程序中为用户添加分数,但在代码“var correctAns=account.profile.score;”上出现错误“Uncaught TypeError:无法读取未定义的属性'profile'。我正在使用AccountsUser软件包 Template.quiz.events({ 'click .next': function (evt,template) { // countdown.start(function () {
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"
}
您需要
使用Meteor.userId()
而不是测验ID更新用户
Id
现在是用户的Id,question\u 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')
}
})
}
该错误意味着找不到用户。这种情况可能是由于以下几个原因造成的:(1)这个。_id
不是您认为的那样,(2)id是有效的,但用户不是当前用户,也没有发布到客户端,等等。