0

几天来,我一直试图弄清楚如何让 forEach 循环在 Meteor 中工作。我有一个QuestionList包含两个数组 ( usersTrue usersFalse) 的集合,供猜测问题的人使用。当我回答问题时,我想通过这两个数组并更改用户分数。我想我真的很接近,但我错误地调用了两个集合之一。

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}} );
        });
    }
},

secret 是存储硬币的数组。

我知道代码并不完美,但我想在清理之前让代码正常工作。我想我提供了足够的信息。我是编程新手,所以如果我遗漏任何有助于回答问题的内容,请告诉我。

4

2 回答 2

0
'modifyQuestionStatus' : function(questionId, answer){
    QuestionList.update(questionId, {$set: {active: false, answer: answer}});

    var usersTrue = _.pluck( QuestionList.find({"_id": questionId}).fetch(), 'usersTrue' );
    var usersFalse = _.pluck( QuestionList.find({"_id": questionId}).fetch(), 'usersFalse' );
    if (answer === true) {
        usersTrue.forEach(function (user) {
            console.log("These users get 100 coins " + user);
            Session.set('userData', user);
            var userData = Session.get('userData');
            Meteor.users.update(userData, {$inc: { "coins.$.coins": 100}} );
        });
        usersFalse.forEach(function (user) {
            console.log("These users lose 100 coins " + user);
            Meteor.users.update({ _id: user}, {$inc: { "coins.$.coins": -100}} );
        });
    } else {
        usersFalse.forEach(function (user) {
            console.log("These users get 100 coins " + user);
            UserList.update({ _id: user}, {$inc: { "coins.coins": 100}} );
        });
        usersTrue.forEach(function (user) {
            console.log("These users lose 100 coins " + user);
            UserList.update({ _id: user}, {$inc: { "coins.coins": -100}} );
        });
    }
},

使用 fetch 并声明 usersTrue 和 usersFalse 作为变量。该代码有效,但未更新硬币文档。

于 2015-01-16T22:41:34.073 回答
0

base 是您可能需要 fetch() 的游标:

var base = QuestionList.find({_id: questionId}).fetch();

甚至 findOne() :

var base = QuestionList.findOne({_id: questionId});
于 2015-01-16T09:13:26.760 回答