我正在构建一个Meteor应用程序,我有竞赛/参赛作品集合。当有人参加比赛时,他们的user_id会被推送到带有$ addToSet的Contest.entered_users数组中。这是代码:
entryInsert: function(entryAttributes) {
check(Meteor.userId(), String);
check(entryAttributes, {
contest_id: String
});
var user = Meteor.user();
var entry = _.extend(entryAttributes, {
user_id: user._id,
user_name: user.profile.name,
submitted: new Date(),
submitted_day: moment().format('MMM D')
});
var currentContest = Contests.findOne(entryAttributes.contest_id);
// Check to make sure that the person has not already entered the giveaway
if (currentContest.entered_users.indexOf(entry.user_id) !== -1) {
throw new Meteor.Error('invalid', "You have already entered the giveaway");
} else {
Contests.update(
currentContest._id,
{
$addToSet: {entered_users: entry.user_id},
$inc: {entries: 1}}
);
}
// Create entry in order to get the entry id
var entryId = Entries.insert(entry, function(err) {
if (err) {
alert(err.reason);
}
});
return {
_id: entryId
}
}
我希望在删除条目时从Contest.entered_users数组中删除person user_id。我试图使用$ pull但它似乎没有工作......当我删除一个条目时,entry.user_id仍然在contest.entered_users数组中。以下是相关代码:
'click .entry-delete': function(e, tmpl) {
e.preventDefault();
var currentEntry = this;
var currentEntryId = this._id;
var contestId = Contests.findOne(currentEntry.contest_id);
// Update the contest by removing the entry's useer_id from entered_users
Meteor.call('contestRemoveEntry', contestId, currentEntry, function(error) {
if (error) {
alert(error.reason);
}
});
Meteor.call('entryRemove', currentEntryId, function(error) {
if(error) {
alert(error.reason);
}
});
}
这是contestRemoveEntry方法:
contestRemoveEntry: function(contestId, currentEntry) {
Contests.update({ _id: contestId }, { $pull: { entered_users: currentEntry.user_id } } );
}
关于为什么这不起作用的任何想法?我尝试过其他SO解决方案,但似乎没有任何工作。
答案 0 :(得分:1)
看来这是让拉动工作的正确方法:
Contests.update(contestId, { $pull: { entered_users: currentEntry.user_id } } );