Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript MongoDB$pull不工作_Javascript_Arrays_Mongodb_Meteor - Fatal编程技术网

Javascript MongoDB$pull不工作

Javascript MongoDB$pull不工作,javascript,arrays,mongodb,meteor,Javascript,Arrays,Mongodb,Meteor,我正在构建一个Meteor应用程序,我有竞赛/参赛作品集。当有人进入竞赛时,他们的用户\u id将被推送到竞赛中。使用$addToSet输入\u用户数组。代码如下: entryInsert: function(entryAttributes) { check(Meteor.userId(), String); check(entryAttributes, { contest_id: String }); var user = Meteor.use

我正在构建一个Meteor应用程序,我有竞赛/参赛作品集。当有人进入竞赛时,他们的用户\u id将被推送到竞赛中。使用$addToSet输入\u用户数组。代码如下:

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\u users数组中删除个人用户\u id。我试图使用$pull,但它似乎不起作用。。。删除条目时,entry.user\u id仍在contest.entered\u 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);
        }
    });
}
以下是ChalletRemoveEntry方法:

contestRemoveEntry: function(contestId, currentEntry) {
    Contests.update({ _id: contestId }, { $pull: { entered_users: currentEntry.user_id } } );
}

你知道为什么这不起作用吗?我尝试过其他SO解决方案,但似乎没有任何效果。

这似乎是使$pull起作用的正确方法:

Contests.update(contestId, { $pull: { entered_users: currentEntry.user_id } } );

_id与competitid的类型相同吗?如果_id类型是ObjectID,那么contesticId也需要是该类型,以便查询匹配。另外,确保currentEntry.user\u id属性值与输入的\u users元素具有相同的类型,如果竞赛id为string并且需要匹配作为ObjectId的_id,请使用:{{u id:ObjectId(竞赛id)}我刚刚尝试使用{u id:ObjectId(竞赛id)}并得到此错误“ObjectId未定义”。知道为什么吗?请确保在使用ObjectId对象之前声明它:var ObjectId=require('mongodb').ObjectId;我刚刚尝试了
Contests.update(contestId,{$pull:{entered_users:currentEntry.user_id})并且它似乎正在工作。。。我可以发誓我早就试过了。