我一直在试图弄清楚如何获得forEach循环在Meteor工作几天。我有一个QuestionList
集合,它有两个数组(usersTrue
usersFalse
),供猜测问题的人使用。当我回答问题时,我想通过这两个数组并更改用户分数。我认为我非常接近,但我错误地调用了两个集合中的一个。无法获取forEach在流星工作
两个集合QuestionList
链接到这个问题的收集和UserList
连接到Meteor.users
我有出版物
Meteor.publish('activeQuestions', function(){
return QuestionList.find({ });
});
Meteor.publish('userNotAnswered', function(){
var currentUserId = this.userId;
return QuestionList.find({active: true, usersTrue: {$nin: [currentUser]},
usersFalse: {$nin: [currentUser]}});
});
Meteor.publish('userAnswer', function(){
var currentUserId = this.userId;
return UserList.find({_id: currentUserId});
});
我对管理侧模板时,我回答这个问题。它用几个参数调用modifyQuestionStatus
方法。
Template.activeQuestionList.events({
'click [data-action=questionTrue]': function() {
// Select the id of the yes button that is clicked
var questionId = this._id;
Session.set('answered', questionId);
// Get the session
var answered = Session.get('answered');
// Update the database without losing any data
Meteor.call('modifyQuestionStatus', answered, true);
},
'click [data-action=questionTrue]': function() {
// Select the id of the yes button that is clicked
var questionId = this._id;
Session.set('answered', questionId);
// Get the session
var answered = Session.get('answered');
// Update the database without losing any data
Meteor.call('modifyQuestionStatus', answered, false);
}
});
最后我有
'modifyQuestionStatus' : function(questionId, answer){
QuestionList.update(questionId, {$set: {active: false, answer: answer}});
var base = QuestionList.find({_id: questionId});
if (answer === true) {
base.usersTrue.forEach(function (user) {
user.update(secret, {$set: {coins: 100}});
});
base.usersFalse.forEach(function (user) {
user.update(secret, {$set: {coins: -100}});
});
} else {
usersFalse.forEach(function (user) {
UserList.update(secret, {$set: {coins: 100}});
});
usersTrue.forEach(function (user) {
UserList.update(secret, {$set: {coins: -100}});
});
}
},
的秘密是,硬币存储在阵列的方法。
我知道代码是不完美的,但我想要得到的代码之前工作我清理它。我想我把足够的信息。我是编程新手,所以让我知道如果我错过任何有助于回答问题的东西。
你会得到什么错误? – ajduke
调用方法'modifyQuestionStatus'时发生异常TypeError:无法调用未定义的方法'forEach'。 –
看起来像'userTrue'或'userFalse'是'undefined'。你是在哪里设置的? – ajduke